是否可以禁用tableView中单元格的“划动删除”手势?如果可以,如何操作?单元格在编辑模式下仍应该可以被编辑,但是划动删除手势应该被禁用。
是否可以禁用tableView中单元格的“划动删除”手势?如果可以,如何操作?单元格在编辑模式下仍应该可以被编辑,但是划动删除手势应该被禁用。
以下是需要做的:
- (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
// Detemine if it's in editing mode
if (self.tableView.editing) {
return UITableViewCellEditingStyleDelete;
}
return UITableViewCellEditingStyleNone;
}
你仍需要使用tableView:commitEditingStyle:forRowAtIndexPath:
方法以便实现删除动画。
这是比iBrad Apps的解决方案更为简洁的解决方案,因为你可以使用默认的self.editButtonItem
而不是自定义按钮。
NO
。 - aopsfan是的。以下代码将禁用它。
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return NO;
}
回复您的评论:
编辑2:如果您想要默认按钮,可以前往@dasblinkenlight发布的链接,但是我的下面的代码将与自定义按钮更好地配合使用。
因此,基本上只需创建一个您想要显示编辑按钮的按钮,然后调用此方法即可。
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
[self.tableview setEditing:editing animated:animated];
}
editButtonItem
的按钮,但有一种方式不同似乎是个坏主意,特别是考虑到它可能会很繁琐...既然有一种支持的方法可以做到你需要的,为什么要这样做呢? - aopsfanoverride func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {
if tableView.editing {
return UITableViewCellEditingStyle.Delete
}
return UITableViewCellEditingStyle.None
}
如果您不想允许某些单元格被滑动和删除,这里有一个解决方案:
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == yourCellIndex) {
return NO;
}
return YES;
}
同时不要忘记实现这个功能,否则它将无法工作:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// do something when deleted
}
}