博客
关于我
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 常见错误
查看>>
mysql 常见问题
查看>>
MYSQL 幻读(Phantom Problem)不可重复读
查看>>
mysql 往字段后面加字符串
查看>>
mysql 快照读 幻读_innodb当前读 与 快照读 and rr级别是否真正避免了幻读
查看>>
MySQL 快速创建千万级测试数据
查看>>
mysql 快速自增假数据, 新增假数据,mysql自增假数据
查看>>
MySql 手动执行主从备份
查看>>
Mysql 批量修改四种方式效率对比(一)
查看>>
mysql 批量插入
查看>>
Mysql 报错 Field 'id' doesn't have a default value
查看>>
MySQL 报错:Duplicate entry 'xxx' for key 'UNIQ_XXXX'
查看>>
Mysql 拼接多个字段作为查询条件查询方法
查看>>
mysql 排序id_mysql如何按特定id排序
查看>>
Mysql 提示:Communication link failure
查看>>
mysql 插入是否成功_PDO mysql:如何知道插入是否成功
查看>>
Mysql 数据库InnoDB存储引擎中主要组件的刷新清理条件:脏页、RedoLog重做日志、Insert Buffer或ChangeBuffer、Undo Log
查看>>
mysql 数据库中 count(*),count(1),count(列名)区别和效率问题
查看>>
mysql 数据库备份及ibdata1的瘦身
查看>>