简体   繁体   English

在numberOfRowsInSection中调用TableView BeginUpdates

[英]Calling TableView BeginUpdates in numberOfRowsInSection

I have a tableview backed by an NSFectchedResultsController and I'm trying to show a custom cell when I have no results from the FRC. 我有一个由NSFectchedResultsController支持的tableview,当我没有FRC的结果时,我试图显示一个自定义单元格。 The problem I'm running into is that BeginUpdates recalls numberOfRowsInSection. 我遇到的问题是BeginUpdates回忆起numberOfRowsInSection。 I want to keep the tableview active (rather than just displaying an image in its place) so that the user can perform a pull to refresh. 我想使tableview保持活动状态(而不是仅在其位置显示图像),以便用户可以执行拉动以刷新。

The code: 编码:

- (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];
}

} }

The problem is the return 1; 问题是返回值1; statement. 声明。 What happens is the first time numberOfRowsInSection is called it sets specialCellShowing = TRUE and hits begin updates, which recalls numberOfRowsInSection. 发生的是第一次调用numberOfRowsInSection,它设置了specialCellShowing = TRUE并点击开始更新,从而调用了numberOfRowsInSection。 The begin updates instance of the method sees that specialCellShowing is true and returns 1 and exits. 该方法的begin updates实例看到specialCellShowing为true并返回1并退出。 Now the insert call is made and then a crash occurs on endUpdates because the tableview thinks that there is 1 cell in the table, 1 cell inserted, and 1 cell previously. 现在进行了插入调用,然后在endUpdates上发生崩溃,因为tableview认为表中有1个单元格,插入了1个单元格,之前有1个单元格。 The other problem is I need that return 1 where it is because in subsequent calls to numberOfRowsInSection I want it to not mess with the table and just return saying I have the one custom cell. 另一个问题是我需要在其中返回1,因为在随后的对numberOfRowsInSection的调用中,我希望它不会弄乱表,只是返回说我有一个自定义单元格。

I guess what I'm wondering is if there is a better way to approach this? 我想我想知道是否有更好的方法来解决这个问题?

You can't mutate the tableview while the tableview is updating its display. 表格视图更新其显示时,您无法对其进行突变。 You don't need to call deleteRowsAtIndex paths unless you're mutating the tableview. 除非您要更改表格视图,否则无需调用deleteRowsAtIndex路径。 You need to have a separate method: one that responds to an event and mutates the tableview's data (and perhaps calls begin/endUpdates and adds or removes rows). 您需要有一种单独的方法:一种响应事件并改变表视图​​数据的方法(也许调用begin / endUpdates并添加或删除行)。 -numberOfRowsInSection should just inspect the backing data and return an answer. -numberOfRowsInSection应该只检查支持数据并返回答案。 The tableview is doing a full update at this point, so adding and removing rows in the tableview is useless at this point anyhow. tableview此时正在进行完整的更新,因此无论如何此时在tableview中添加和删除行都是无用的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM