Table左滑删除并添加多个按钮实例

跟标题一样,就是写了table的左划删除功能,并且可以添加多按钮,当然下面也多写了很多代理方法,算是福利吧。

感谢飞飞大神的博客供这篇文章的学习:侧滑删除

下面直接上代码注释都写好了:


#import "ViewController.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate,UIActionSheetDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self makeTable];
    
    // Do any additional setup after loading the view, typically from a nib.
}

#pragma mark 做table
- (void)makeTable{
    UITableView *demoTab = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    
    demoTab.delegate = self;
    
    demoTab.dataSource = self;
    
    [self.view addSubview:demoTab];
}

#pragma mark table代理
//有几段
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{
    return 1;
}

//有几行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{
    return 10;
}

//cell
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{
    static NSString *iden = @"iden";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:iden];
    
    if (cell==nil) {
        
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:iden];
        
    }
    
    cell.textLabel.text = @"我就是个占位的";
    
    return cell;
    
}

//对编辑的状态下提交的事件响应
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

{
    NSLog(@"d");
}

//让表格可以修改,滑动可以修改
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath

{
    return YES;
}

//让行可以移动
-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

//table向左滑动时的代理
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

{
    NSLog(@"我向左滑动啦~");
    return UITableViewCellEditingStyleDelete;
}


//好吧滑动以后的几个按钮这里来写
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath

{
    //建立一个按钮叫删除
    UITableViewRowAction *layTopRowAction1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        
        //点击删除时使用的方法
        NSLog(@"删除");
        
        //允许开启编辑单元格
        [tableView setEditing:YES animated:YES];

    }];
    //给删除按钮设置一个颜色
    layTopRowAction1.backgroundColor = [UIColor redColor];
    
    
    //建立第二个按钮更多
    UITableViewRowAction *layTopRowAction2 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"更多" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {

        //点击更多出现一个Sheet选择
        NSLog(@"更多");

        UIActionSheet *actionSheet = [[UIActionSheet alloc]
                                      
                                      initWithTitle:nil
                                      
                                      delegate:self
                                      
                                      cancelButtonTitle:@"取消"
                                      
                                      destructiveButtonTitle:nil
                                      
                                      otherButtonTitles:@"创建待办", @"标为未读", @"标为红旗", @"移动", @"这是垃圾邮件",nil];
        
        actionSheet.actionSheetStyle = UIBarStyleBlackOpaque;
        
        [actionSheet showInView:self.view];
        
        //允许开启编辑单元格
        [tableView setEditing:YES animated:YES];

    }];
    
    //设置第二个按钮的颜色
    layTopRowAction2.backgroundColor = [UIColor grayColor];
    
    //最后保存好两个按钮统一放在数组中传递回去
    NSArray *arr = @[layTopRowAction1,layTopRowAction2];
    
    return arr;
    
}

#pragma mark sheet的代理
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    
    switch (buttonIndex) {
            
        case 0:NSLog(@"创建待办");
            
            break;
            
        case 1:NSLog(@"标为未读");
            
            break;
            
        case 2:NSLog(@"标为红旗");
            
            break;
            
        case 3:NSLog(@"移动");
            
            break;
            
        case 4:NSLog(@"这是垃圾邮件");
            
            break;
            
        case 5:NSLog(@"取消");
            
            break;
            
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


打完收工~
感谢观看,学以致用更感谢!



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值