iOS 测滑删除

2018-04-25  本文已影响0人  Shrock

1.单个删除按钮

单个删除按钮样式

代码:

//先要设Cell可编辑

- (BOOL)tableView:(UITableView*)tableView canEditRowAtIndexPath:(NSIndexPath*)indexPath{

    return YES;

}

//定义编辑样式

- (UITableViewCellEditingStyle)tableView:(UITableView*)tableView editingStyleForRowAtIndexPath:(NSIndexPath*)indexPath {

    return UITableViewCellEditingStyleDelete;

}

//设置进入编辑状态时,Cell不会缩进

- (BOOL)tableView: (UITableView*)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath*)indexPath {

    return NO;

}

//点击删除

- (void)tableView:(UITableView*)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath*)indexPath {

  //根据需求作删除数据的操作  ps:当然要请求接口删除对应的服务器数据

  [self.dataArray removeObjectAtIndex:indexPath.section];

  [tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationAutomatic];

}

2:测滑多个按钮

测滑多个按钮样式

// 自定义左滑显示编辑按钮

-(NSArray*)tableView:(UITableView*)tableView editActionsForRowAtIndexPath:(NSIndexPath*)indexPath

{

UITableViewRowAction *rowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault                                                                         title:@"删除"handler:^(UITableViewRowAction*_Nonnullaction,NSIndexPath*_NonnullindexPath)  { 

       //作相应的操作

       NSLog(@"删除");

    }];

 rowAction.backgroundColor = [UIColor redColor];//设置背景颜色

 UITableViewRowAction *rowAction2 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault                                                                         title:@"嘿嘿"handler:^(UITableViewRowAction*_Nonnullaction,NSIndexPath*_NonnullindexPath) {

    //作相应的操作

    NSLog(@"嘿嘿");

  }];

    rowAction2.backgroundColor = [UIColor blueColor];//设置背景颜色

 UITableViewRowAction *rowAction3 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault                         title:@"叼"handler:^(UITableViewRowAction*_Nonnullaction,NSIndexPath*_NonnullindexPath) {

      //作相应的操作   

      NSLog(@"叼");

}];

    rowAction3.backgroundColor = [UIColor blackColor];//设置背景颜色

    NSArray*arr =@[rowAction,rowAction2,rowAction3];

    return  arr;

}

3.总结

自带的测滑删除已经很6。

上一篇 下一篇

猜你喜欢

热点阅读