iOS 自定义cell多个删除按钮样式

tableview代理方法

#pragma mark 在滑动手势删除某一行的时候,显示出更多的按钮
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 添加一个删除按钮
    UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除"handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        NSLog(@"点击了删除");
        
         1. 更新数据
        [_allDataArray removeObjectAtIndex:indexPath.row];
        // 2. 更新UI
        [tableView deleteRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationAutomatic];
    }];
    
    // 删除一个置顶按钮
    UITableViewRowAction *topRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"置顶"handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        NSLog(@"点击了置顶");
        
        // 1. 更新数据
        [_allDataArray exchangeObjectAtIndex:indexPath.row withObjectAtIndex:0];
        
        // 2. 更新UI
        NSIndexPath *firstIndexPath = [NSIndexPath indexPathForRow:0inSection:indexPath.section];
        [tableView moveRowAtIndexPath:indexPathtoIndexPath:firstIndexPath];
    }];
    topRowAction.backgroundColor = [UIColor blueColor];
    // 将设置好的按钮放到数组中返回
    return @[deleteRowAction, topRowAction];
}

自定义cell内

- (void)layoutSubviews{
    [super layoutSubviews];
    //遍历子视图,找出左滑按钮
    for (UIView *subView in self.subviews)
    {
        if([subView isKindOfClass:NSClassFromString(@"UITableViewCellDeleteConfirmationView")])
        {
            for (UIButton *btn in subView.subviews) {
                if ([btn isKindOfClass:[UIButton class]]) {
                    //更改左滑标签按钮样式
                    if ([btn.titleLabel.text isEqualToString:@"置顶"]) {
                        [btn setTitle:@"" forState:UIControlStateNormal];
                        [btn setBackgroundImage:[UIImage imageNamed:@"2.jpg"] forState:UIControlStateNormal];
                    }else if([btn.titleLabel.text isEqualToString:@"删除"]){
                        //更改左滑详情按钮样式
                        [btn setTitle:@"" forState:UIControlStateNormal];
                        [btn setBackgroundImage:[UIImage imageNamed:@"1.jpg"] forState:UIControlStateNormal];
                    }
                }
            }
        }
    }
}

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

推荐阅读更多精彩内容