你的位置:首页 > 操作系统

[操作系统]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];    }  }}

效果图:

昆明旅游团报价什么时候去昆明旅游最好去昆明旅游最佳路线昆明旅游景点推荐昆明跟团旅游报价多少八月国内机票淡季价纷纷出炉 广东“寻找最美导游”活动正式启动 布达拉宫门票多少?旅游淡旺季是什么时候? 广州南站禁单车入闸惹争议 高铁上单车能当行李吗? 西岭雪山今年第一场雪,点燃深秋最后激情 2012年第五届四川红叶生态旅游节即将开幕 细数中国五大冰雪圣境,体验不一样的冬天 韩国枫叶季 营造独属秋天的浪漫氛围 澳门特色购物指南 【泸沽湖旅游】—泸沽湖特色美食介绍 澳门购物环境 魅力无法挡 彰显绝妙的川西古镇(图) 深圳石岩附近有什么好玩的地方? 深圳图书馆在哪里? 深圳哪里有漂流? 五月份有什么节日?五月份有哪些重要节日? MJD42CT4G Datasheet MJD42CT4G Datasheet MJE15029 Datasheet MJE15029 Datasheet MJE15031 Datasheet MJE15031 Datasheet 在首尔 在首尔 在首尔 ecuador ecuador ecuador 基督城 基督城 基督城