我试图阻止删除我的 UITableView 控件的每个部分中的每一行的最后一行。我编写了目前可以正常工作的代码。
在 UITableView 的编辑模式下,是否有办法防止删除按钮出现在特定部分的特定行?
代码如下:
- (void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
NSInteger section = indexPath.section;
NSInteger row = indexPath.row;
// If table view is asking to commit a delete command...
if ( editingStyle == UITableViewCellEditingStyleDelete) {
// Prevent deleting the last row
int length = [[[MyItemStore sharedStore] getItemsForGivenSection:section] count];
// PREVENT LAST ROW FROM DELETING
if ( row == length) {
return;
}
NSArray *items = [[MyItemStore sharedStore] getItemsForGivenSection:section];
MyItem *item = items[row];
[[MyItemStore sharedStore] removeItemFromSection:item fromSection:section];
//Also remove that row from Table view with animation
[tableView deleteRowsAtIndexPaths: @[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
最佳答案
你可以使用这样的东西:
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
//Return FALSE for the last row
if (indexPath.row == [tableView numberOfRowsInSection:indexPath.section] - 1)
return FALSE;
}
//Return TRUE for all other rows
return TRUE;
}
关于ios - UITableView - 控制或防止删除部分中的特定行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24976897/