我正在项目中实现一个可编辑的表格 View 。当我点击左侧滑动后显示的红色 Delete
按钮时,应用程序崩溃了。控制台日志在这里:
*** -[UITableView _endCellAnimationsWithContext:],/BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit/UIKit-3512.60.7/UITableView.m:1716 断言失败
在我的表格 View 中,有很多部分,但每个部分只有一行,因为我需要部分标题来分隔每个单元格。这是我的 UITableViewDelegate
和 UITableViewDataSource
的实现。
#pragma mark - Delegate Methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return self.dataSource.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 15.f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return 0.01f;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
MyNewsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellReuseID forIndexPath:indexPath];
// Configure the cell...
cell.selectionStyle = UITableViewCellSelectionStyleNone;
[cell setCellWithModel:self.dataSource[indexPath.section]];
return cell;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleDelete;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[self.dataSource removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
最佳答案
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
删除该行后仍然返回 1。
您必须删除该部分
[tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationFade]
关于ios - [UITableView _endCellAnimationsWithContext :],/BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit/UIKit-3512.60.7/UITableView.m:1716,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36738459/