UITableView多選全選


自定義cell和取到相應的cell就行了

TableViewCell.h

#import <UIKit/UIKit.h>

@interface TableViewCell : UITableViewCell {
    BOOL _checked;
    UIImageView *_checkedImage;
}

- (void)setChecked:(BOOL)checked;

@end

TableViewCell.m

#import "TableViewCell.h"

@implementation TableViewCell

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

- (void)layoutSubviews {
    [super layoutSubviews];
    _checkedImage.frame = CGRectMake(10, 10, 29, 29);
}

- (void)setChecked:(BOOL)checked {
    if (checked) {
        _checkedImage.image = [UIImage imageNamed:@"Selected"];
    }else {
        _checkedImage.image = [UIImage imageNamed:@"Unselected"];
    }
  _checked = checked; }
- (void)awakeFromNib { // Initialization code } - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; // Configure the view for the selected state } @end

ViewController.m

#import "ViewController.h"
#import "TableViewCell.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate> {
    UITableView *_tableView;
}

@property (nonatomic,strong)NSMutableArray *array;
@property (nonatomic,strong)NSMutableArray *checkedArray;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initDataSource];
    self.view.backgroundColor = [UIColor lightGrayColor];
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 100, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds))];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [self.view addSubview:_tableView];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    [button setTitle:@"全選" forState:UIControlStateNormal];
    [button setTitle:@"取消" forState:UIControlStateSelected];
    button.frame = CGRectMake(10, 10, 100, 50);
    [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
}

- (void)initDataSource {
    _checkedArray = [NSMutableArray array];
    for (int i = 0; i < self.array.count; i ++) {
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        [dic setValue:@"NO" forKey:@"checked"];
        [_checkedArray addObject:dic];
    }
}

#pragma mark - 懶加載

- (NSMutableArray *)array {
    if (!_array) {
        _array = [[NSMutableArray alloc]initWithObjects:@"1",@"2",@"3",@"4",@"5", nil];
    }
    return _array;
}

#pragma mark - 事件監聽

- (void)buttonPressed:(UIButton *)sender {
    sender.selected = !sender.selected;
    NSArray *anArrayOfIndexPath = [NSArray arrayWithArray:[_tableView indexPathsForVisibleRows]];
    for (int i = 0; i < [anArrayOfIndexPath count]; i++) {
        NSIndexPath *indexPath= [anArrayOfIndexPath objectAtIndex:i];
        //取得對應的cell
        TableViewCell *cell = (TableViewCell*)[_tableView cellForRowAtIndexPath:indexPath];
        NSUInteger row = [indexPath row];
        NSMutableDictionary *dic = [_checkedArray objectAtIndex:row];
        if (sender.selected) {
            [dic setObject:@"YES" forKey:@"checked"];
            [cell setChecked:YES];
        }else {
            [dic setObject:@"NO" forKey:@"checked"];
            [cell setChecked:NO];
        }
    }
}

#pragma mark - <UITableViewDataSource,UITableViewDelegate>

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellID = @"cellID";
    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[TableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    NSUInteger row = indexPath.row;
    [self cellChecked:cell row:row isSelected:NO];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    TableViewCell *cell = (TableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
    NSUInteger row = indexPath.row;
    [self cellChecked:cell row:row isSelected:YES];
}
#pragma mark - others

/**
 *  點擊,和加載cell的時候進行判斷,從而改變cell的選中狀態
 *
 *  @param cell       自定義的cell
 *  @param row        tableView的下標
 *  @param isSelected 是否是點擊
 */

- (void)cellChecked:(TableViewCell *)cell row:(NSUInteger)row isSelected:(BOOL)isSelected{
    
    NSMutableDictionary *dic = [_checkedArray objectAtIndex:row];
    if ([[dic objectForKey:@"checked"] isEqualToString:@"NO"]) {
        if (isSelected) {
            [dic setObject:@"YES" forKey:@"checked"];
            [cell setChecked:YES];
        }else {
            [dic setObject:@"NO" forKey:@"checked"];
            [cell setChecked:NO];
        }
    }else {
        if (!isSelected) {
            [dic setObject:@"YES" forKey:@"checked"];
            [cell setChecked:YES];
        }else {
            [dic setObject:@"NO" forKey:@"checked"];
            [cell setChecked:NO];
        }
    }
}

效果圖:

這是以前寫的老版本,新版本請看:

http://www.cnblogs.com/hxwj/p/4536499.html 

新版demo下載地址:http://pan.baidu.com/s/1o6DpN0u

修改了一個全選的bug:https://github.com/WuJiForFantasy/UITableViewChooseDelete-.git

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM