您的位置:首页 > 产品设计 > UI/UE

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

2017-05-20 11:48 501 查看

单个左滑按钮的实现

如果左滑后只需要一个按钮,只要在实现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;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios ios开发
相关文章推荐