2012-02-29 15 views
1

我有一個表格視圖與部分。iPhone:UITableView部分

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
     return numberHigherThen0; 
} 
中的cellForRowAtIndexPath方法設置我行

根據部分

- (UITableViewCell *)tableView:(UITableView *)aTableView 
     cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (0 == indexPath.section) { 
     //setup rows 
    } esle { 
    //setup rows 
    } 
} 

數部分和行的部分中動態地改變。但部分的數字總是> 0.所以在cellForRowAtIndexPath indexPath.section == 0總會有這種情況,對吧?但有時cellForRowAtIndexPath沒有通過案例indexPath.section == 0!爲什麼?什麼時候可以是真的?當cellForRowAtIndexPath可以從1開始計數節?

更新: 第2節中沒有行

回答

1

numberOfSectionsInTableView:指定部分的數量 - 但是,的部分從編號。

所以,如果你在你的numberOfSectionsInTableView返回3,你將有三個部分 - 部分0,第1節和第2節

+0

那麼爲什麼indexPath.section == 0有時從來就不是真的? – Jim 2012-02-29 12:49:57

+0

該部分可能不可見,或該部分可能根本沒有行? – 2012-02-29 12:51:28

+0

該部分是可見的,它有1行 – Jim 2012-02-29 12:55:44

0

tableView:cellForRowAtIndexPath:當表視圖需要在給定的刷新細胞只叫索引路徑。

因此,如果第0部分在應用程序啓動時可見,那麼應該至少調用一次部分== 0。在隨後的刷新中,如果UITableView認爲不需要刷新它,則可以跳過第0部分。這可能是你所看到的。

使用reloadRowsAtIndexPaths:withRowAnimation:reloadSections:withRowAnimation:等告訴UITableView行已經改變,並且需要刷新它。

+0

「因此,如果第0部分在您的應用程序啓動時可見,是的,您應該至少調用一次部分== 0。」但我沒有,這裏是問題 – Jim 2012-02-29 12:52:23

+0

你是否爲本節返回一個大於0的數字到'tableView:numberOfRowsInSection:'?如果該部分沒有行,那麼UITableView將不會嘗試獲取它的行... – 2012-02-29 13:14:28