2014-09-29 27 views
0

對於部分在我的UITableView數量的正確數量,我使用以下功能:numberOfRowsInSection沒有返回行

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return _matchCenterArray.count; 
} 

這正常工作,並顯示在表中部分的正確的號碼。它指的是看起來像這樣的_matchCenterArray

(
     { 
     "Top 3" =   (
         { 
       "Image URL" = "http://thumbs4.ebaystatic.com/m/mu05CM1bactFTAWZjesohNg/140.jpg"; 
       "Item URL" = "http://www.ebay.com/itm/Nexus-7-Latest-Model-16GB-Wi-Fi-7in-Black-2nd-Generation-2013-/281443895415?pt=US_Tablets"; 
       Price = "170.0"; 
       Title = "Nexus 7 (Latest Model) 16GB, Wi-Fi, 7in - Black (2nd) Generation 2013"; 
      }, 
         { 
       "Image URL" = "http://thumbs4.ebaystatic.com/m/mjkaCfDBdTQ6S-VTD0kFimA/140.jpg"; 
       "Item URL" = "http://www.ebay.com/itm/Asus-Google-Nexus-7-2nd-Generation-Tablet-7-16GB-Android-4-3-/351173222631?pt=US_Tablets"; 
       Price = "165.59"; 
       Title = "Asus Google Nexus 7 2nd Generation Tablet 7\" 16GB Android 4.3 "; 
      }, 
         { 
       "Image URL" = "http://thumbs4.ebaystatic.com/m/mjIENDWxrHTfcrO_Tmu4-zw/140.jpg"; 
       "Item URL" = "http://www.ebay.com/itm/Google-Nexus-7-16GB-HD-K008-NEXUS7-ASUS-2B16-2nd-Gen-Tablet-Priority-Ship-/301315740555?pt=US_Tablets"; 
       Price = "164.99"; 
       Title = "Google Nexus 7 16GB HD K008 NEXUS7 ASUS-2B16 2nd Gen Tablet Priority Ship"; 
      }, 
         { 
       "Search Term" = "nexus 7 16gb"; 
      } 
     ); 
    }, 
     { 
     "Top 3" =   (
         { 
       "Image URL" = "http://thumbs3.ebaystatic.com/m/mbSjXw608gtlLhYC5GbrbOg/140.jpg"; 
       "Item URL" = "http://www.ebay.com/itm/rolex-datejust-/301318985642?pt=Wristwatches"; 
       Price = "400.0"; 
       Title = "rolex datejust"; 
      }, 
         { 
       "Image URL" = "http://thumbs3.ebaystatic.com/m/mx2eBCLXBEY30DXIIMrm_MQ/140.jpg"; 
       "Item URL" = "http://www.ebay.com/itm/VINTAGE-LADIES-ROLEX-STANDARD-17J-GOLD-FILLED-WATCH-WORKING-NICE-/111466728842?pt=Wristwatches"; 
       Price = "349.99"; 
       Title = "VINTAGE LADIES ROLEX STANDARD 17J GOLD FILLED WATCH WORKING NICE "; 
      }, 
         { 
       "Image URL" = "http://thumbs2.ebaystatic.com/m/mj7-GblU-4Al2X5q0sRvkfw/140.jpg"; 
       "Item URL" = "http://www.ebay.com/itm/Rolex-17000-Quartz-Datejust-BLACK-DIAL-5035-/271610085029?pt=Wristwatches"; 
       Price = "400.0"; 
       Title = "Rolex 17000 Quartz Datejust BLACK DIAL 5035"; 
      }, 
         { 
       "Search Term" = rolex; 
      } 
     ); 
    } 
) 

據我所看到的,它是前3數組的數組。在這種情況下,有兩個排名前3陣列,這導致了兩個部分。就每節的行數而言,我希望它是Top 3數組中的字典對象的數量。

在這種情況下,如果我沒有弄錯,它們在這個數組中似乎都有4個字典。我試過用下面的函數做這個,但是它只顯示每個部分一行:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    NSArray *currentSectionArray = _matchCenterArray[section]; 
    return currentSectionArray.count; 

    return 3; 
} 
+0

你試過調試它嗎?在numberOfRowsInSection函數中放置一個斷點,看看currentSectionArray是什麼? – sha 2014-09-29 17:37:32

回答

1

數據源看起來我的方式是有字典的數組,因此與部分相關聯的每個對象是字典,而不是一個數組,並獲得該數組字典你需要訪問的對象的關鍵,在這種情況下,「前3」獲得該部分的陣列:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    NSDictionary *currentSectionDictionary = _matchCenterArray[section]; 
    NSArray *top3ArrayForSection = currentSectionDictionary[@"Top 3"]; 
    return top3ArrayForSection.count; 
} 
+1

這是正確的答案,或短版本:返回[_matchCenterArray [部分] [@「Top 3」] count]; – 2014-09-29 18:33:50

+1

也許吧,但如果你像我一樣分開步驟,它也會*大大地容易跟隨正在發生的事情。編寫代碼時更容易一步步完成,以及其他人是否稍後還會遇到代碼,以便能夠理解數據源設置。更短並不總是更好:) – Mike 2014-09-29 18:41:30

0

_matchCenterArray [section]應該是一個字典,是否正確?難道是你將NSDictionary分配給一個NSArray變量? 也許嘗試

NSArray *currectSectionArray = [_matchCenterArray[section] allKeys]; 
return [currentSectionArray count];