iOS 表中左滑cell显示多个按钮

/ 0评 / 0

在以前我们侧滑编辑表的时候有些操作很不方便,但是从iOS8 开始就简单多了。
1. 我们在使用一些应用的时候,在滑动一些联系人的某一行的时候,会出现删除、置顶、更多等等的按钮,在iOS8之前,我们都需要自己去实现。But,到了iOS8,系统已经写好了,只需要一个代理方法和一个类就搞定了
2. iOS8的协议多了一个方法,返回值是数组的tableView:editActionsForRowAtIndexPath:方法,我们可以在方法内部写好几个按钮,然后放到数组中返回,那些按钮的类就是UITableViewRowAction
tableView:editActionsForRowAtIndexPath: // 设置滑动删除时显示多个按钮
UITableViewRowAction // 通过此类创建按钮

3. 在UITableViewRowAction类,我们可以设置按钮的样式、显示的文字、背景色、和按钮的事件(事件在Block中实现)
4. 在代理方法中,我们可以创建多个按钮放到数组中返回,最先放入数组的按钮显示在最右侧,最后放入的显示在最左侧
5. 注意:如果我们自己设定了一个或多个按钮,系统自带的删除按钮就消失了...

所以我们只需要实现下面的代理方法就可以了

- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSMutableDictionary *smallDic = [self.adressListArray objectAtIndex:indexPath.row];
    YOLog(@"点击了删除 %@", smallDic);
    //添加一个删除按钮
    UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        NSLog(@"回调方法");
    }];
    
    //添加一个设为默认按钮
    UITableViewRowAction *setDefaultRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"设为默认" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
       NSLog(@"回调方法");
    }];
    
    return @[deleteRowAction,setDefaultRowAction];
}

这样就可以实现侧滑cell进行多种自定义操作了~

评论已关闭。