簡體   English   中英

在numberOfRowsInSection中調用TableView BeginUpdates

[英]Calling TableView BeginUpdates in numberOfRowsInSection

我有一個由NSFectchedResultsController支持的tableview,當我沒有FRC的結果時,我試圖顯示一個自定義單元格。 我遇到的問題是BeginUpdates回憶起numberOfRowsInSection。 我想使tableview保持活動狀態(而不是僅在其位置顯示圖像),以便用戶可以執行拉動以刷新。

編碼:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if ([self.fetchedResultsController.fetchedObjects count] == 0) {
    if (!specialCellShowing) {
        specialCellShowing = TRUE;
        [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
        [self.tableView beginUpdates];
        [self.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForItem:0 inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
        [self.tableView endUpdates];
    }
    return 1;
}
else {
    if (specialCellShowing) {
        specialCellShowing = FALSE;
        [self.tableView beginUpdates];
        [self.tableView deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForItem:0 inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
        [self.tableView endUpdates];
    }
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
    return [self.fetchedResultsController.fetchedObjects count];
}

}

問題是返回值1; 聲明。 發生的是第一次調用numberOfRowsInSection,它設置了specialCellShowing = TRUE並點擊開始更新,從而調用了numberOfRowsInSection。 該方法的begin updates實例看到specialCellShowing為true並返回1並退出。 現在進行了插入調用,然后在endUpdates上發生崩潰,因為tableview認為表中有1個單元格,插入了1個單元格,之前有1個單元格。 另一個問題是我需要在其中返回1,因為在隨后的對numberOfRowsInSection的調用中,我希望它不會弄亂表,只是返回說我有一個自定義單元格。

我想我想知道是否有更好的方法來解決這個問題?

表格視圖更新其顯示時,您無法對其進行突變。 除非您要更改表格視圖,否則無需調用deleteRowsAtIndex路徑。 您需要有一種單獨的方法:一種響應事件並改變表視圖​​數據的方法(也許調用begin / endUpdates並添加或刪除行)。 -numberOfRowsInSection應該只檢查支持數據並返回答案。 tableview此時正在進行完整的更新,因此無論如何此時在tableview中添加和刪除行都是無用的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM