当我们在使用tableview时,往往需要在cell左滑时显示一个或是多个按钮,但系统默认的只可显示一个,如常见的删除按钮,那么当我们的需求要求要有多个按钮时又该怎么办呢,我们往下看。

首先,现看看系统的按钮(只显示一个按钮时)

//设置cell左滑后的删除按钮文字
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    return @"删除";
}
//点击cell的删除按钮后调用该方法
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [callRecordsArr removeObjectAtIndex:indexPath.row];
        //数据源删除对应元素要在tableview删除对应的cell之前
        [tableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
        [NSKeyedArchiver archiveRootObject:callRecordsArr toFile:CALLRECORDSCACHEPATH];
    }
}
 
如需要显示多个按钮,参照如下代码(注意:当我们使用自定义按钮后,如上的系统默认方法将失去作用)
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    returntrue;
}
 
-(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(nonnullNSIndexPath *)indexPath{
    UITableViewRowAction *action1 = [UITableViewRowActionrowActionWithStyle:UITableViewRowActionStyleNormaltitle:@"加入黑名单"handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        //在block中实现相对应的事件
    }];
    UITableViewRowAction *action2 = [UITableViewRowActionrowActionWithStyle:UITableViewRowActionStyleDestructivetitle:@"删除"handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        [callRecordsArrremoveObjectAtIndex:indexPath.row];
        //数据源删除对应元素要在tableview删除对应的cell之前
        [tableView deleteRowsAtIndexPaths:[NSMutableArrayarrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
        [NSKeyedArchiverarchiveRootObject:callRecordsArrtoFile:CALLRECORDSCACHEPATH];
    }];
    action2.backgroundColor = [UIColorpurpleColor];
    注意:1、当rowActionWithStyle的值为UITableViewRowActionStyleDestructive时,系统默认背景色为红色;当值为UITableViewRowActionStyleNormal时,背景色默认为淡灰色,可通过UITableViewRowAction的对象的.backgroundColor设置;
        2、当左滑按钮执行的操作涉及数据源和页面的更新时,要先更新数据源,在更新视图,否则会出现无响应的情况
    UITableViewRowAction *toTop = [UITableViewRowActionrowActionWithStyle:UITableViewRowActionStyleDestructivetitle:@"置顶"handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        //更新数据
        [callRecordsArrexchangeObjectAtIndex:indexPath.rowwithObjectAtIndex:0];
        //更新页面
        NSIndexPath *firstIndexPath = [NSIndexPathindexPathForRow:0inSection:indexPath.section];
        [tableView moveRowAtIndexPath:indexPath toIndexPath:firstIndexPath];
    }];
       //此处UITableViewRowAction对象放入的顺序决定了对应按钮在cell中的顺序
    return@[toTop,action2,action1];
}
 
显示图形如下

在这要补充一点的就是,我在查相关资料时,上面自定义按钮时,都加上了“

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

{

}”这个方法,而我在实验测试时发现没有这个方法也同样能实现预期功能,并且也没有发现什么bug,因此本次总结的方法只供参考,若是哪位大神知道这一点的,请不吝赐教!

最新文章

  1. CP
  2. SQL 提高查询效率
  3. WPF RichTextBox 做内容展示框 滚动条控制判定是否阅读完成
  4. .htaccess下Flags速查表
  5. interview que
  6. iOS 视图,动画渲染机制探究
  7. Http协议总结
  8. [Python爬虫] Selenium+Phantomjs动态获取CSDN下载资源信息和评论
  9. Arcgis for android 离线查询
  10. 使用Visual Studio Code开发Asp.Net Core WebApi学习笔记(六)-- 依赖注入
  11. <一道题>abc+cba=1333,求满足条件的abc的值,隐含条件a!=0,c!=0
  12. Asp.net基础知识
  13. MTV模型
  14. Codeforces Round #345(Div. 2)-651A.水题 651B.。。。 651C.去重操作 真是让人头大
  15. 安卓高级6 SnackBar
  16. DFS迷宫递归所有路径 新手入门
  17. EF 外键不显示、如何让外键显示!增、删、改 操作时,外键不显示,只显示导航属性!
  18. E. Superhero Battle
  19. Linux 典型应用之远程连接SSH
  20. 最好用的 IntelliJ 插件 Top 10

热门文章

  1. 遇到 HTTP 错误 403.14 - Forbidden?
  2. 改用C++生成自动化数据表
  3. table-cell实现宽度自适应布局
  4. chrome扩展程序开发
  5. C#读取数据库中的表
  6. Wave - 花たん 音乐
  7. 四大组件之ContentProvider
  8. 你不知道的HttpHandler相关知识
  9. 迷惑很久,仅以个人想法谈谈MVC架构,希望大家多给点意见
  10. Spring 3.0 AOP (一)AOP 术语