UITableView设置Cell左滑多个按钮(编辑,删除,置顶等)

一、iOS7不支持cell多个按钮这个时候可以使用一个三方库JZTableViewRowAction,引用类扩展文件并实现其代理方法

JZTableViewRowAction下载地址:http://download.csdn.net/download/chunjunlu/9506344

- (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {

    __weak typeof(self) weakself = self;

    void(^deleteBabyAction)(UITableViewRowAction *, NSIndexPath *) = ^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {

        [weakself deleteAction:self.babyList[indexPath.row]];

        [weakself.babyListTable setEditing:false animated:true];

    };

  UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"删除" handler:deleteBabyAction];

    deleteAction.backgroundColor = [UIColor colorWithHexString:@"ff4545"];

    void(^setDefaultBabyAction)(UITableViewRowAction *, NSIndexPath *) = ^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {

        [weakself setDefaultBaby:self.babyList[indexPath.row]];

        [self.babyListTable setEditing:false animated:true];

    };    

    UITableViewRowAction *setDefaultAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"设为当前" handler:setDefaultBabyAction];

    setDefaultAction.backgroundColor = [UIColor colorWithHexString:@"ffa902"];

    return @[deleteAction,setDefaultAction];

}

二、iOS8以后可以直接使用系统设置

* tableView:editActionsForRowAtIndexPath: // 设置滑动删除时显示多个按钮

* UITableViewRowAction // 通过此类创建按钮

* 1. 我们在使用一些应用的时候,在滑动一些联系人的某一行的时候,会出现删除、置顶、更多等等的按钮,在iOS8之前,我们都需要自己去实现。But,到了iOS8,系统已经写好了,只需要一个代理方法和一个类就搞定了

* 2. iOS8的协议多了一个方法,返回值是数组的tableView:editActionsForRowAtIndexPath:方法,我们可以在方法内部写好几个按钮,然后放到数组中返回,那些按钮的类就是UITableViewRowAction

* 3. 在UITableViewRowAction类,我们可以设置按钮的样式、显示的文字、背景色、和按钮的事件(事件在Block中实现)

* 4. 在代理方法中,我们可以创建多个按钮放到数组中返回,最先放入数组的按钮显示在最右侧,最后放入的显示在最左侧

* 5. 注意:如果我们自己设定了一个或多个按钮,系统自带的删除按钮就消失了...

/设置滑动时显示多个按钮
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
//添加一个删除按钮
UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:(UITableViewRowActionStyleDestructive) title:@删除 handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@点击了删除);
//1.更新数据
[self.dataArray removeObjectAtIndex:indexPath.row];
//2.更新UI
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationAutomatic)];
}];
//删除按钮颜色
deleteAction.backgroundColor = [UIColor cyanColor];
//添加一个置顶按钮
UITableViewRowAction *topRowAction =[UITableViewRowAction rowActionWithStyle:(UITableViewRowActionStyleDestructive) title:@置顶 handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@点击了置顶);
//1.更新数据
[self.dataArray exchangeObjectAtIndex:indexPath.row withObjectAtIndex:];
//2.更新UI
NSIndexPath *firstIndexPath =[NSIndexPath indexPathForRow: inSection:indexPath.section];
[tableView moveRowAtIndexPath:indexPath toIndexPath:firstIndexPath];
}];
//置顶按钮颜色
topRowAction.backgroundColor = [UIColor magentaColor];
//--------更多
UITableViewRowAction *moreRowAction = [UITableViewRowAction rowActionWithStyle:(UITableViewRowActionStyleNormal) title:@更多 handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
DetailViewController *detailVC = [[DetailViewController alloc]init];
[self.navigationController pushViewController:detailVC animated:YES]; }];
//背景特效
//moreRowAction.backgroundEffect = [UIBlurEffect effectWithStyle:(UIBlurEffectStyleDark)];
//----------收藏
UITableViewRowAction *collectRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@收藏handler:^(UITableViewRowAction *action,NSIndexPath *indexPath) {
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@收藏 message:@收藏成功 delegate:self cancelButtonTitle:@确定 otherButtonTitles:nil, nil]; [alertView show];
[alertView release];
}];
//收藏按钮颜色
collectRowAction.backgroundColor = [UIColor greenColor]; //将设置好的按钮方到数组中返回
return @[deleteAction,topRowAction,moreRowAction,collectRowAction];
// return @[deleteAction,topRowAction,collectRowAction];
}
上一篇:vue导出excel


下一篇:Sticks_dfs