关于TableViewCell的删除按钮的自定义

系统的删除按钮是红色的,而往往公司想要一个和主色调一样的颜色。这就需要自己再定义了。
代码如下

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleDelete;
}
//改变删除按钮的样式
- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewRowAction * delect = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        UIAlertController *alertView = [UIAlertController alertControllerWithTitle:@"提示" message:@"删除这条消息" preferredStyle:UIAlertControllerStyleAlert];
        [alertView addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
#pragma mark 测试阶段先这样写,之后再改
            [_list removeObjectAtIndex:indexPath.row];
            [_MainTab reloadData];
        }]];
        [self presentViewController:alertView animated:true completion:nil];
    }];
    delect.backgroundColor = XHRGBA(91, 178, 187, 1);
    return @[delect];
}

当然也可以实现多个按钮的效果,比如置顶/收藏/删除。因为返回的是数组。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容