【发布时间】:2014-09-12 08:55:20
【问题描述】:
我看到了很多关于它的问题,但没有找到解决问题的方法。
我有一个带有自定义单元格的表格视图。在每个单元格中,我都有一个计时器。当时间到时,我发送一条消息删除该行(这不是我唯一使用接收消息的地方)。
- (NSInteger)numberOfSectionsInTableView:(UITableView *)theTableView{
return 1;
}
- (void) timesup:(NSNotification *)notification{
Data *data = notification.userInfo[@"data"];
NSUInteger index = [datas indexOfObject:data];
NSArray *deleteIndexPaths = [[NSArray alloc] initWithObjects:
[NSIndexPath indexPathForRow:index inSection:0],
nil];
[datas removeObjectAtIndex:index];
[self.tableView deleteRowsAtIndexPaths:deleteIndexPaths withRowAnimation:UITableViewRowAnimationFade];
}
- (NSInteger)tableView:(UITableView *)theTableView numberOfRowsInSection:(NSInteger)section
{
if(datas.count > 0){
return datas.count;
}
// Display a message when the table is empty
UILabel *messageLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
messageLabel.text = @"No data, pull to refresh";
messageLabel.textColor = [UIColor blackColor];
messageLabel.numberOfLines = 0;
messageLabel.textAlignment = NSTextAlignmentCenter;
[messageLabel sizeToFit];
self.tableView.backgroundView = messageLabel;
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
return 0;
}
我收到了这个错误(取决于我在玩应用程序时有多少数据):
无效更新:第 0 节中的行数无效。更新后现有节中包含的行数 (4) 必须等于更新前该节中包含的行数 (6),加上或减去从该节插入或删除的行数(0 插入,1 删除)加上或减去移入或移出该节的行数(0 移入,0 移出)。
当我有很多数据超时时发生错误。
注意:如果我不使用“deleteRowsAtIndexPaths”,只需从数据中删除并重新加载表即可完美运行
【问题讨论】:
-
尝试使用异步串行队列来转移删除逻辑(在您的通知方法中)。这样,删除将按 FIFO 顺序进行序列化。或者更好地检查我在下面给出的代码。
-
要显示的任何代码(一周前开始 iOS 开发:D)
-
建议使用
beginUpdates和endUpdates。此外,请确保删除后的行/节数等于数据源数组中的项目数。
标签: ios objective-c uitableview