实现自定义左滑删除修改背景颜色以及字体颜色,目前ios9 和ios11有两种方式可以实现左滑删除功能
1、iOS9
- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView setEditing:NO animated:YES];
UITableViewRowAction *action = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath){
//TODO 进行删除相关的代码
}];
//设置背景颜色
action.backgroundColor = [UIColor colorWithHexString:@"#FFBE11"];
//设置删除按钮文字颜色
[[UIButton appearanceWhenContainedInInstancesOfClasses:@[[LikeMyUsersViewController class]]] setTitleColor:[UIColor colorWithHexString:@"#0A0A0A"] forState:UIControlStateNormal];
return @[action];
}
2、iOS11
- ( UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath API_AVAILABLE(ios(11.0)){
//删除
if (@available(iOS 11.0, *)) {
UISwipeActionsConfiguration *config;
UIContextualAction *deleteRowAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive title:@"删除" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
//TODO 进行删除相关的代码逻辑
completionHandler (YES);
}];
//删除背景颜色
deleteRowAction.backgroundColor = [UIColor colorWithHexString:@"#FFBE11"];
//删除文字颜色设置
[[UIButton appearanceWhenContainedInInstancesOfClasses:@[[LikeMyUsersViewController class]]] setTitleColor:[UIColor colorWithHexString:@"#0A0A0A"] forState:UIControlStateNormal];
config = [UISwipeActionsConfiguration configurationWithActions:@[deleteRowAction]];
config.performsFirstActionWithFullSwipe = NO;
return config;
}else{
return nil;
}
}