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

来源:互联网 发布:win2008流量监控软件 编辑:程序博客网 时间:2024/06/05 06:15

跟标题一样,就是写了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
原创粉丝点击