UITableViewCell 多選和全選(checkBoxCell)

思路1html

 1、單選數組

一、建立一個currentIndexPath爲了記錄當前選擇的indexPath。
二、 若是是記錄上一次選中的內容,能夠經過模型中的是否選中來初始化indexPathForRow。
三、首先判斷建立的indexPath是否等於當前點擊的indexPath。
四、若是不等於數組中的模型所有置爲NO。
五、再將當前點擊的indexPath傳給建立的currentIndexPath,並刷新當前表格(傳建立的currentIndexPath)。
六、經過點擊的indexPath取出當前模型並取反,再經過點擊的indexPath刷新表格
代碼以下:
一、_indexPath = [NSIndexPath indexPathForRow:[group.brand indexOfObject:viewModel] inSection:[_dataArray indexOfObject:group]];//取出選中的行及列,viewModel表示是選中的模型,若是進入頁面不須要設置上一次選中的內容能夠不寫這一步。
二、NSIndexPath *selectIndexPath = _indexPath;
   if (selectIndexPath && selectIndexPath != indexPath) {//判斷建立的indexPath是否不等於點擊的indexPath
       for (GMeSaleAreaGroupViewModel *GviewModel in _dataArray) {//若是不等於置爲NO
          for (GMeSaleAreaViewModel*viewModel  in GviewModel.brand) {
                 viewModel.isSelect = NO;
             }
         }
//刷新沒有選中的表格
    [tableView reloadRowsAtIndexPaths:@[_indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
  }
_indexPath = indexPath; //點擊的indexPath傳給上一個點擊的indexPath
GMeSaleAreaViewModel *viewModel = [GroupViewModel.brand objectAtIndex:indexPath.row]; //取出當前點擊的indexPath
viewModel.isSelect = !viewModel.isSelect;//取反 [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];//刷新當前點擊的表格

  

2、全選ide

  1.建立可變數組,存儲全部未選中狀態(NO)的布爾值按鈕,點擊時改變其狀態,並傳入按鈕的狀態。atom

 3、多選spa

  1.建立Cell時,從數組中取出相應的值,傳給cell,若是爲YES,不然爲NO.code

  2.點擊cell時,從數組中取出相應的值,取反,而後刷新該行。orm

4、代碼先行htm

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

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self loadUI];
    [self loadDataWithSelected:NO];
}
- (void)loadUI{
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 50,self.view.bounds.size.width,self.view.bounds.size.height - 50) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    
    UIButton *customBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [customBtn setFrame:CGRectMake(50,0 , 200, 50)];
    [customBtn setBackgroundColor:[UIColor lightGrayColor]];
    [customBtn setTitle:@"多選" forState:UIControlStateNormal];
    [customBtn setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
    [customBtn setTitle:@"全選" forState:UIControlStateSelected];
    [customBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:customBtn];
}
- (void)loadDataWithSelected:(BOOL)isSelected{
    
    NSMutableArray *arr = [NSMutableArray array];
    for (NSInteger i = 0; i< 20; i++) {
        [arr addObject:@(isSelected)];
    }
    self.dataArray = arr;
    [_tableView reloadData];
}
- (void)btnClick:(UIButton *)sender{
    
    [self loadDataWithSelected:sender.selected];
    sender.selected = !sender.selected;

}
#pragma mark --- tableview
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.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];
    }
    if (self.dataArray.count > indexPath.row) {
        NSNumber *selected = [self.dataArray objectAtIndex:indexPath.row];
        [cell setChecked:selected.boolValue];
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (self.dataArray.count > indexPath.row) {
        NSNumber *selected = self.dataArray [indexPath.row];
        self.dataArray[indexPath.row] = @(!selected.boolValue);
        [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
   
}

cell代碼同下;blog

思路2get

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

思路分析

前期準備:1.建立兩個數組:一個數據數組array,一個狀態數組checkArray,(初始化時每個值爲NO).

     2.多選,初始化時存入全部NO值給checkArray,點擊cell時取出每一值取反,全選則是根據按鈕狀態,若是是選中所有爲YES,不然所有爲NO。

一.全選

  1.點擊時反選按鈕狀態,取出全部indexPath未選中狀態給新的數組,而後遍歷取出相應的indexpath,cell和row;

      2.取出每個值,若是是選中,設置爲YES,不然設置爲NO.

二.多選(點擊cell時,取出相應的row和cell,根據row從checkArray取出一項,並設置cell狀態)

  1.當cell點擊時,根據點擊cell的row從checkArray取出相應的項,改變其值,若是是YES,設置爲YES,不然設置爲NO。

三.代碼先行

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;
}
相關文章
相關標籤/搜索