博客
关于我
iOS开发:UITableView实现侧滑删除cell的功能
阅读量:139 次
发布时间:2019-02-27

本文共 1814 字,大约阅读时间需要 6 分钟。

       总结了一下UITableView的代理方法的使用情况,介绍一下UITableView实现侧滑删除cell的功能,只要实现了删除cell的几个代理方法,就可以轻而易举的实现侧滑删除的效果。这里只介绍cell的侧滑删除的几个代理方法,不再介绍怎么展示cell数据等代理方法。

       1.首先设置cell可以编辑

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

    return YES;

}

        2.设置编辑的样式

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

    return UITableViewCellEditingStyleDelete;

}

        3.修改编辑按钮文字

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {

    return @"取消收藏";  //我这里需要设置成“取消收藏”而不是“删除”,文字可以自定义

}

        4.设置进入编辑状态的时候,cell不会缩进

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

    return NO;

}

        5.点击删除的实现。特别提醒:必须要先删除了数据,才能再执行删除的动画或者其他操作,不然会引起崩溃。

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

    //实现删除操作

    LiveCollectionModel *collectionModel = _dataArray[indexPath.row];

    LiveUserModel *user = [LiveLocal user];

    [MBProgressHUD showMessage:@"" inView:self.view mode:MBProgressHUDModeIndeterminate withBlock:^(MBProgressHUD *hud) {

        [KingHttpTool POST:Mine_DelCollect_Url

                    params:@{@"token":user.token,

                             @"collect_id":collectionModel.collect_id

                             }

                   success:^(id responseObject) {

                       NSInteger status = [[responseObject valueForKey:@"status"] integerValue];

                       NSString *msg = [responseObject valueForKey:@"msg"];

                       if (status == 1) {

                           [hud hideWithSuccess:msg completionBlock:^{

                               //删除数据,和删除动画

                               [_dataArray removeObject:collectionModel];

                               [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];

                           }];

                       }else {

                           [hud hideWithFailure:msg completionBlock:nil];

                       }

                   } failure:^(NSString *errorMsg) {

                       NSLog(@"%@",errorMsg);

                   }];

    }];

}

       

你可能感兴趣的文章
mysql技能梳理
查看>>
MySQL报Got an error reading communication packets错
查看>>
Mysql报错Can‘t create/write to file ‘/tmp/#sql_3a8_0.MYD‘ (Errcode: 28 - No space left on device)
查看>>
MySql报错Deadlock found when trying to get lock; try restarting transaction 的问题解决
查看>>
MySQL报错ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘
查看>>
Mysql报错Packet for query is too large问题解决
查看>>
mysql报错级别_更改MySQL日志错误级别记录非法登陆(Access denied)
查看>>
Mysql报错:too many connections
查看>>
MySQL报错:无法启动MySQL服务
查看>>
mysql授权用户,创建用户名密码,授权单个数据库,授权多个数据库
查看>>
mysql排序查询
查看>>
MySQL排序的艺术:你真的懂 Order By吗?
查看>>
MySQL排序的艺术:你真的懂 Order By吗?
查看>>
Mysql推荐书籍
查看>>
Mysql插入数据从指定选项中随机选择、插入时间从指定范围随机生成、Navicat使用存储过程模拟插入测试数据
查看>>
MYSQL搜索引擎
查看>>
mysql操作数据表的命令_MySQL数据表操作命令
查看>>
mysql操作日志记录查询_如何使用SpringBoot AOP 记录操作日志、异常日志?
查看>>
MySQL支持的事务隔离级别,以及悲观锁和乐观锁的原理和应用场景?
查看>>
mysql支持表情
查看>>