iOS开发笔记(4) -- UITableView的左滑删除与自定义

来源:互联网 发布:centos虚拟机不能上网 编辑:程序博客网 时间:2024/04/29 01:33
无可争议,在iOS开发中UITableView的使用当之无愧是最频繁的视图之一,同时当使用UITableView是操作cell左滑删除也会随之被经常使用;

不废话上代码;
首先说一下单一按钮情况:

//设置你的滑块标题- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{    return @"Delete";}//如果你是删除功能那么一定不要忘记删除数据源中的对应数据,不然...血的教训- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{    // 从数据源中删除    [self.array removeObjectAtIndex:indexPath.row];    // 从列表中删除    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];}//将tableview设为可编辑状态- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{    return YES;}

接下来说一下多个滑块的情况:

-(NSArray<UITableViewRowAction*>*)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{    UITableViewRowAction *renameAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Rename" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {        NSLog(@"重命名");    }];    UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Delete" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {        // 从数据源中删除        [self.array removeObjectAtIndex:indexPath.row];        // 从列表中删除        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];    }];    renameAction.backgroundColor = UIColorFromRGB(0x87caf8);    deleteAction.backgroundColor = UIColorFromRGB(0xfc665e);    NSArray *actionArr = @[renameAction,deleteAction];    return actionArr;}
1 0
原创粉丝点击