我是 Objective-C 的新手。所以我有一个表格 View ,该部分中有 5 行(5 个单元格),我想允许用户删除它们,但是当我单击删除按钮时应用程序不断崩溃。它崩溃的原因是因为我在该部分中有 5 行而不是返回 [self.myListItems count] 如何在保留该部分中的 5 行的同时更正下面的代码?
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[self.myListItems removeObjectAtIndex:indexPath.row];
[self saveList];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
错误是:-[UITableView _endCellAnimationsWithContext:] 中的断言失败
最佳答案
在 ViewDidLoad 中:
self.tableView.allowsMultipleSelectionDuringEditing = NO;
和
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return YES if you want the specified item to be editable.
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
//add code here for when you hit delete
[self.myListItems removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
[self saveList];
}
}
请记住 - 如果您想删除一个部分中的最后一项,则需要删除整个部分(否则可能会导致部分计数错误并引发此异常)。
希望这可以帮助。
关于ios - 如何删除tableView单元iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38238262/