UI层面需要做的事情
为tableView设置好delegate后,需要实现以下delegate方法。
允许左滑row
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
设置左滑row后显示的内容
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleDelete;
}
实现以上方法的作用是实现左滑显示删除(Delete)按钮。
逻辑层面需要做的事情
出了让tableView的一行可以左滑出现删除按钮,还需要定义用户点击删除按钮后的行为。
还是通过实现UITableViewDelagate方法:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
}
如果只有一种EditingStyle且EditingStyle为删除,直接在方法里填写删除后的行为即可;如果有多种EditingStyle,可以在进入方法之后先进行一步判断:
if (editingStyle == UITableViewCellEditingStyleDelete) {
}
在if内部定义删除后的行为。