UITableViewCell侧滑按钮的设置

前言:以前只知道实现一个代理方法可以实现UITableViewCell侧滑可以出现一个系统默认的删除按钮,前两天才发现这个按钮可以进行一些设置,并且可以添加其他按钮并执行一些代码操作,上代码

1--我们先简单的创建一个UITableView并在cell上显示点东西
ViewController.m文件
#import "ViewController.h"
#import "NextViewController.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
/**tableView*/
@property (nonatomic , strong) UITableView *tableView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 60, self.view.frame.size.width , self.view.frame.size.height) style: UITableViewStylePlain];
    //self.tableView.backgroundColor = [UIColor orangeColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
      self.tableView.rowHeight = 80;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:self.tableView];
}

#pragma mark -- 数据源方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.textLabel.text = [NSString stringWithFormat:@"row --- %ld",(long)indexPath.row];
    cell.backgroundColor = [UIColor blueColor];
     return  cell;
}


2--实现代理方法并设置侧滑按钮
#pragma mark -- 代理方法
//实现了这个方法就有滑动的删除按钮了
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}
//这个方法就是可以自己添加一些侧滑出来的按钮,并执行一些命令和按钮设置
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    //设置按钮(它默认第一个是修改系统的)
    UITableViewRowAction *action = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"咬我" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
           NSLog(@"咬我啊");
  }];
 //设置按钮(它默认第一个是修改系统的)
    UITableViewRowAction *action1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"跳转" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
       //执行跳转到下个界面操作 
        NextViewController *vc = [[NextViewController alloc]init];
           [self presentViewController:vc animated:YES completion:nil];
     }];

    action1.backgroundColor = [UIColor colorWithRed:0.9305 green:0.3394 blue:1.0 alpha:1.0];
   return @[action,action1];
 }

@end

现在基本可以利用cell干更多的事情了
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,198评论 4 61
  • 概述在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似...
    liudhkk阅读 9,086评论 3 38
  • Day 1 新街口→ 总统府→江宁织造博物馆→老门东→夫子庙 第一站 新街口 ️新接口地铁站有20多个出口,一定要...
    阿_喵阅读 900评论 1 2
  • 话说在某博某信上流传着一大段儿微信张老板讲话。 实质内容篇幅较长,就不拷贝当搬运工了,另外也避嫌剽窃文章可耻行径。...
    OOS阅读 250评论 0 0
  • 临到晚上了,不开心。本来特别满足的一天,结果毁在了他的朋友圈上。 是的,我只有心情不好的时候,才会写东西,这些乱七...
    日月之约阅读 507评论 0 0