IOS开发入门:实现UITableViewCell的左滑操作(编辑、删除等)

来源:互联网 发布:环境污染测试软件 编辑:程序博客网 时间:2024/04/28 08:59

单个左滑按钮的实现

如果左滑后只需要一个按钮,只要在实现UITableViewDelegate的Controller中实现下面的委托方法:

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {    //处理删除的操作}

左滑删除

默认的编辑模式是UITableViewCellEditingStyleDelete。如果想改变作画按钮上的文字可以用:

-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {    return @"更新";}

修改按钮标题

想要改变按钮的背景颜色可以参照这篇文章

自定义多个左滑按钮

实现下面的委托方法,按钮的背景颜色和名称都可以自定义:

//多个自定义的按钮- (nullable NSArray<UITableViewRowAction *> *) tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {    UITableViewRowAction *uploadAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"Upload" handler:^(UITableViewRowAction *action,NSIndexPath *path) {        NSLog(@"upload事件");        //需要实现的操作    }];    uploadAction.backgroundColor = [UIColor cyanColor];    UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Delete" handler:^(UITableViewRowAction *action,NSIndexPath *path) {        NSLog(@"delete事件");       //需要实现的操作    }];    NSArray *actionArray = [NSArray arrayWithObjects:deleteAction,uploadAction,nil];    return actionArray;}


多个按钮

阅读全文
0 0
原创粉丝点击