码迷,mamicode.com
首页 > 其他好文 > 详细

UITableViewCell 多选和全选(checkBoxCell)

时间:2016-05-02 18:39:02      阅读:397      评论:0      收藏:0      [点我收藏+]

标签:

原文地址: http://www.cnblogs.com/hxwj/p/4532172.html

思路分析

前期准备:1.创建两个数组:一个是真正数据的不可变数组array,一个是自定义可变数组checkArray,(存储一个key对应array.count个value,值为NO).

     2.给cell创建两个属性:一个用来存储多选或全选的状态,一个用来控制选中和未选中。

       3.在cell中给外界暴露一个设置状态方法,然后根据外界传进来的状态来设置不同的图像状态。

一.全选

  1.这里使用按钮的选中和未选中状态来控制,点击时反选状态。

  2.把tableView中所有未选中cell的indexPath存储给一个不可变数组[_tableView indexPathsForVisibleRows]。

  3.然后遍历这个数组,分别取出cell,row,indexPath;然后通过row从checkArray取出每一项。

  4.如何按钮为选中状态设置value值为YES,并设置cell状态为YES,否则value值为NO,并设置cell的状态为NO.

二.多选

  1.当cell点击时调用一个自定义的方法,传入cell,row,还有是否是点击状态。

  2.通过row从checkArray数组取出一个字典,然后判断这个字典的key的value值。

  3.如果是NO的话,设置为YES,否则设置为NO.

以下是原文的思路:

  (3.如果是NO的话,如果是单击设置为YES,并设置cell的状态为YES,否则设置为NO.

  4.如果是YES的话,如果是单击设置为NO,并设置cell的状态为NO,否则设置为YES.)

三.贴代码

cell

#import "CheckBoxCell.h"
@interface CheckBoxCell(){
    
    BOOL        _isSelect;
    UIImageView *_chectImageView;
    
}
@end
@implementation CheckBoxCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        _chectImageView = [[UIImageView alloc] init];
        _chectImageView.image = [UIImage imageNamed:@"normal"];
        [self.contentView addSubview:_chectImageView];
    }
    return self;
}

- (void)layoutSubviews{
    
    [super layoutSubviews];
    [_chectImageView setFrame:CGRectMake(10, 10,30, 30)];
}
- (void)setchecked:(BOOL)checked{
    
    _isSelect = checked;
    if(_isSelect){
        
        _chectImageView.image = [UIImage imageNamed:@"select"];
    }else{
        _chectImageView.image = [UIImage imageNamed:@"normal"];
    }
  

VIewControll

#import "ChectBoxViewController.h"
#import "CheckBoxCell.h"
@interface ChectBoxViewController ()<UITableViewDelegate,UITableViewDataSource>{
    
    
    UITableView   *_tableView;
}
@property (nonatomic, strong)NSArray *array;
@property (nonatomic, strong)NSMutableArray *chectArray;
@end

@implementation ChectBoxViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self loadUI];
    [self loadData];
}
#pragma mark --- viewDidLoad
- (void)loadUI{
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,50, self.view.bounds.size.width, self.view.bounds.size.height) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView setBackgroundColor:[UIColor lightGrayColor]];
    [self.view addSubview:_tableView];
    
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:@"多选" forState:UIControlStateNormal];
    [btn setTitle:@"全选" forState:UIControlStateSelected];
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [btn setFrame:CGRectMake(10, 10, 100, 50)];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.view addSubview:btn];
}
- (void)loadData{
    
    _chectArray = [NSMutableArray array];
    for (NSInteger i = 0; i < self.array.count; i++) {
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        [dic setValue:@"NO" forKey:@"checked"];
        [_chectArray addObject:dic];
    }
}
#pragma mark --- tableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.array.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *ID = @"checkboxCell";
    CheckBoxCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[CheckBoxCell  alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    return  cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
     CheckBoxCell*cell = (CheckBoxCell *)[tableView cellForRowAtIndexPath:indexPath];
    NSInteger row = indexPath.row;
    [self cellChecked:cell row:row isSelected:YES];
}

#pragma mark --- response methods
/**
 *  点击,和加载cell的时候进行判断,从而改变cell的选中状态
 *
 *  @param cell     自定义cell
 *  @param row      tableView的下标
 *  @param selected 是否是点击
 */
- (void)cellChecked:(CheckBoxCell *)cell row:(NSInteger)row isSelected:(BOOL)selected{
    
    
    NSMutableDictionary *dic = [_chectArray objectAtIndex:row];
    if ([[dic objectForKey:@"checked"] isEqualToString:@"NO"]) {
        if (selected) {
            [dic setObject:@"YES" forKey:@"checked"];
            [cell setchecked:YES];
        }
    }else{
        
        if (selected) {
            [dic setObject:@"NO" forKey:@"checked"];
            [cell setchecked:NO];
        }
    }
}
- (void)btnClick:(UIButton *)sender{//全选
    
    sender.selected = !sender.selected;
    
    NSArray *arrayOfIndexPath = [NSArray arrayWithArray:[_tableView indexPathsForVisibleRows]];
    for (NSInteger i = 0; i < arrayOfIndexPath.count; i++) {
        NSIndexPath *indexPath = [arrayOfIndexPath objectAtIndex: i];
        CheckBoxCell *cell = (CheckBoxCell *)[_tableView cellForRowAtIndexPath:indexPath];
        NSUInteger row = [indexPath row];
        NSMutableDictionary *dic = [_chectArray objectAtIndex:row];
            if (sender.selected) {
                [dic setObject:@"YES" forKey:@"checked"];
                [cell setchecked:YES];
            }else{
                [dic setObject:@"NO" forKey:@"checked"];
                [cell setchecked:NO];
            }
        }
}
- (NSArray *)array{
    if (_array == nil) {
        _array = [[NSMutableArray alloc] initWithObjects:@"1",@"2",@"3",@"4",@"5", nil];
    }
    return _array;
}

 

UITableViewCell 多选和全选(checkBoxCell)

标签:

原文地址:http://www.cnblogs.com/TheYouth/p/5452781.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!