在Ios8之后这个api实现cell滑动删除方便多了,还可以设置按钮个数以及各种属性:
-(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(nonnullNSIndexPath *)indexPath{
UITableViewRowAction *delete = [UITableViewRowActionrowActionWithStyle:UITableViewRowActionStyleDefaulttitle:@"删除"handler:^(UITableViewRowAction *_Nonnull action,NSIndexPath * _Nonnull indexPath) {
[_arrayremoveObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSMutableArrayarrayWithObject:indexPath]withRowAnimation:UITableViewRowAnimationFade];
}];
UITableViewRowAction *top = [UITableViewRowActionrowActionWithStyle:UITableViewRowActionStyleDefaulttitle:@"置顶"handler:^(UITableViewRowAction *_Nonnull action,NSIndexPath * _Nonnull indexPath) {
//...
}];
top.backgroundColor = [UIColorgrayColor];
return@[delete,top];
}
//系统默认为'delete',修改为'删除'
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
return@"删除";
}
//设置编辑样式为(删除)
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
returnUITableViewCellEditingStyleDelete;
}
//提交操作
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle ==UITableViewCellEditingStyleDelete) {
[_arrayremoveObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSMutableArrayarrayWithObject:indexPath]withRowAnimation:UITableViewRowAnimationFade];
} elseif (editingStyle == UITableViewCellEditingStyleInsert) {
//...
}
}