2013-04-21 88 views
0

我有一個控件,部分或完全改變tableView的內容。發生變化後,我設置一個標誌tableViewContentHasChanged當UITableView完全重新加載

BOOL tableViewContentHasChanged = YES; 
[self.tableView reloadData]; 
tableViewContentHasChanged = NO; 

我的問題出現在tableView:viewForHeaderInSection:;它被稱爲表視圖重新加載,所以我的國旗在該方法內無效。

簡而言之:當表格有完全重新加載時,什麼是正確的方式來觀察,所以我可以設置標誌爲NO?而且,我可能做錯了什麼?

+2

你能解釋這個標誌的用途嗎? – 2013-04-21 10:38:37

+0

..目的是,當內容(以及索引)發生變化時,我需要更新headerView的block屬性中的一些數據。顯然,我不想一直這樣做,只有當變化發生時纔會這樣做。這是目的 – user1244109 2013-04-21 10:44:30

+0

如果我是你,我會檢查UITableView的「beginUpdates」和「endUpdates」方法。也許它比reloadData更可靠。 – 2013-04-21 11:25:18

回答

2

我認爲處理這個最好的方法是在數據模型中所提到別人,但如果你真的需要做到這一點,你可以做到以下幾點:

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行。

希望這會有所幫助。

+0

謝謝瑪麗亞姆。雖然我已經以同樣的方式解決了它,並且來到這裏寫相同的答案=)。接受爲正確 – user1244109 2013-04-21 15:07:05