UI - 修改tableView多选默认蓝色选中按钮 , 全选 , 取消全选

经常会有需求 , 更改tableView多选的默认按钮颜色 , 默认为蓝色 , 很多情况下可能会根据自己项目的主题色进行修改 . 简单粗暴 , 直接上代码 .


重写Cell的布局方法 , 依次遍历子控件 , 修改默认颜色

- (void)layoutSubviews
{
    [super layoutSubviews];

    [self.subviews enumerateObjectsUsingBlock:^(__kindof UIControl * _Nonnull control, NSUInteger idx, BOOL * _Nonnull stop) {

        if ([control isMemberOfClass:NSClassFromString(@"UITableViewCellEditControl")]) {

            for (UIView *view in control.subviews) {

                if ([view isKindOfClass:[UIImageView class]]) {

                    UIImageView *imageView = (UIImageView *)view;
                    if (self.selected) {
                    //设置选中时的照片
                        imageView.image = JYLoadImage(@"JY_selected");
                    }else{
                    //设置未选中时的照片
                        imageView.image = JYLoadImage(@"JY_select");
                    }
                }
            }
        }
    }];
}

全选和取消全选 , 遍历数据源 , 创建 NSIndexPatch

//清空选中数组
    [self.selectedArray removeAllObjects];
    for (NSInteger index = 0; index < self.trashArray.count; index ++) {

        //取消全选
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:index];
        if (self.isAllSected) {
            //取消选中该行
            [self.trashList deselectRowAtIndexPath:indexPath animated:NO];
        }else{

            //全选
            //加入选中数组
            [self.selectedArray addObject:indexPath];
            //选中该行
            [self.trashList selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
        }
    }

UI - 修改tableView多选默认蓝色选中按钮 , 全选 , 取消全选_第1张图片

你可能感兴趣的:(iOS,Objective-c,UI)