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

UITableView多选全选

时间:2015-05-27 00:58:11      阅读:114      评论:0      收藏:0      [点我收藏+]

标签:

自定义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];
        }
    }
}

效果图:

技术分享

 

UITableView多选全选

标签:

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

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