我認爲處理這個最好的方法是在數據模型中所提到別人,但如果你真的需要做到這一點,你可以做到以下幾點:
據Apple's documentation,唯一可見的部分/細胞被重新加載當你調用reloadData
,所以你需要知道什麼時候最後一個可見標題渲染,所以你設置:
tableViewContentHasChanged = YES;
[self.tableView reloadData];
然後在的cellForRowAtIndexPath:獲得最後顯示的索引,並將其存儲在一個成員變量:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//Your cell creating code here
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"TryCell"];
//Set last displayed index here
lastLoadedSectionIndex = indexPath.section;
NSLog(@"Loaded cell at %@",indexPath);
return cell;
}
時viewForHeaderInSection:
叫你就會知道這是在重載情況下的最後一個頭這樣的話:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
//Create or customize your view
UIView *headerView = [UIView new];
//Toggle tableViewContentHasChanged when it's the last index
if (tableViewContentHasChanged && section == lastLoadedSectionIndex) {
tableViewContentHasChanged = NO;
NSLog(@"Reload Ended");
}
return headerView;
}
請注意,如果最後一個可見的區段具有這種方法纔有效至少1行。
希望這會有所幫助。
你能解釋這個標誌的用途嗎? – 2013-04-21 10:38:37
..目的是,當內容(以及索引)發生變化時,我需要更新headerView的block屬性中的一些數據。顯然,我不想一直這樣做,只有當變化發生時纔會這樣做。這是目的 – user1244109 2013-04-21 10:44:30
如果我是你,我會檢查UITableView的「beginUpdates」和「endUpdates」方法。也許它比reloadData更可靠。 – 2013-04-21 11:25:18