一、单按钮左滑动删除(IOS8之前)
只要实现了- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;这个代理方法就拥有了滑动删除的功能
单按钮滑动删除方法一:
//滑动删除执行的代理方法
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"点击左滑出现的Delete按钮执行的操作");
[_array removeObjectAtIndex:indexPath.row];
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
}
//修改默认Delete按钮的文字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
return @"删除";
}
二、多按钮组左滑动删除(IOS8之后)
我们在使用一些应用的时候,在滑动一些联系人的某一行的时候,会出现删除、置顶、更多等等的按钮,在iOS8之前,我们都需要自己去实现。但是,到了iOS8,,只需要一个代理方法和一个类就搞定了
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewRowAction *action = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"关注" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
NSLog(@"点击了删除");
// 1. 更新数据
[DataArray removeObjectAtIndex:indexPath.row];
// 2. 更新UI
[tableView deleteRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationAutomatic];
}];
action.backgroundColor=[UIColor greenColor];
UITableViewRowAction *action1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
}];
UITableViewRowAction *action2 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"置顶" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
}];
return @[action1,action,action2];
}
单按钮左滑动删除只需要返回一个元素的数组就行了,因此单按钮滑动删除方法二就来了
单按钮滑动删除方法二:
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewRowAction *action = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"关注" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
NSLog(@"点击了关注");
}];
action.backgroundColor=[UIColor greenColor];
return @[action];
}
三、IOS11新增的滑动删除
IOS11新增了两个代理方法,可以给这些按钮添加图片了。如果实现了新增的Swipe actions的代理方法将会取代(tableView: editActionsForRowAtIndexPath:)
1.向左滑动在右边显示
- (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath
使用说明:
- (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath{
/* UIContextualActionStyle有两种类型:
UIContextualActionStyleNormal,//置顶、删除、已读都可以使用该类型
UIContextualActionStyleDestructive//删除类型可使用,从左到右一直滑cell,不用点击删除按钮就可以直接执行删除操作
*/
//1.创建UIContextualAction对象
UIContextualAction *deleteRowAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive title:@"delete" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
//[self.titleArr removeObjectAtIndex:indexPath.row];
completionHandler (YES);
}];
//2.给滑动按钮添加背景、图片
deleteRowAction.image = [UIImage imageNamed:@"icon_del"];
deleteRowAction.backgroundColor = [UIColor blueColor];
//3.返回滑动按钮
UISwipeActionsConfiguration *config = [UISwipeActionsConfiguration configurationWithActions:@[deleteRowAction]];
return config;
}
2、向右滑动在左边显示(新增的右滑功能,以前都是左滑)
左滑是系统删除按钮,右滑是自定义的滑动按钮
- (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView leadingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath{
UIContextualAction *deleteRowAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleNormal title:@"取消" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
//[self.titleArr removeObjectAtIndex:indexPath.row];
completionHandler (YES);
}];
deleteRowAction.image = [UIImage imageNamed:@"icon_del"];
deleteRowAction.backgroundColor = [UIColor blueColor];
UIContextualAction *RowAction1 = [UIContextualAction contextualActionWithStyle: UIContextualActionStyleNormal title:@"关注" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
//[self.titleArr removeObjectAtIndex:indexPath.row];
completionHandler (YES);
}];
RowAction1.image = [UIImage imageNamed:@"icon_del"];
RowAction1.backgroundColor = [UIColor greenColor];
UISwipeActionsConfiguration *config = [UISwipeActionsConfiguration configurationWithActions:@[deleteRowAction,RowAction1]];
return config;
}