自定义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(, , , );
} - (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(, , 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(, , , );
[button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button]; } - (void)initDataSource {
_checkedArray = [NSMutableArray array];
for (int i = ; 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:@"",@"",@"",@"",@"", nil];
}
return _array;
} #pragma mark - 事件监听 - (void)buttonPressed:(UIButton *)sender {
sender.selected = !sender.selected;
NSArray *anArrayOfIndexPath = [NSArray arrayWithArray:[_tableView indexPathsForVisibleRows]];
for (int i = ; 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 ;
} - (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

 

UITableView多选全选的更多相关文章

  1. Dynamic CRM 2013学习笔记(二十六)报表设计:Reporting Service报表 动态参数、参数多选全选、动态列、动态显示行字体颜色

    上次介绍过CRM里开始报表的一些注意事项:Dynamic CRM 2013学习笔记(十五)报表入门.开发工具及注意事项,本文继续介绍报表里的一些动态效果:动态显示参数,参数是从数据库里查询出来的:参数 ...

  2. 基于JQ的多选/全选/反选及获取选中的值

    <!-- author:青芒 --> <!DOCTYPE html> <html lang="en"> <head> <met ...

  3. Jquery 多选全选/取消 选项卡切换 获取选中的值

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  4. vue-element 动态单选多选全选

    实现效果如图 数据格式如下: pps: [{"code":"6","createTime":"2018-09-07 00:00:0 ...

  5. checkout 多选 全选(亲测有效)

    <input type="button" id="btn1" value="全选"> <input type=" ...

  6. js分类多选全选

    效果如图: HTML代码: <div class="form-group quanxian-wrap"> <label>项目</label> & ...

  7. wpf中为DataGrid添加checkbox支持多选全选

    项目中用到DataGrid, 需要在第一列添加checkbox, 可以多选.全选. 其中涉及的概念DataTemplate, DataGridCellStyle, DataGridCellContro ...

  8. layui 复选框checkbox 实现全选全选

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. Antd组件Table树型多选全选问题

    组件库antd里面的树型选择不能做到勾选父组件然后一起勾选子组件情况,我也不知道是组件库的问题还是原本设计就是这样 刚好组件库存在rowselection的配置项,既然存在拓展方法,又遇到需求,那么就 ...

随机推荐

  1. java遍历Map的几种方式

    1.遍历map的几种方式:private Hashtable<String, String> emails = new Hashtable<String, String>(); ...

  2. Percona-Xtrabackup 2.3.3 死锁不再堵塞备份(二)

    在percona-xtrabackup2.1.9下备份: session one:root(yoon)> show tables;+----------------+| Tables_in_yo ...

  3. wget的下载与安装使用

    wget的下载与安装:下载地址:ftp://ftp.cs.cuhk.edu.hk/pub/gnu/gnu/wget安装:先把wget下载到的tar文件解压,然后cd到wget目录下# ./config ...

  4. Python运行Google App Engineer时出现的UnicodeDecodeError错误解决方案

    #Python运行Google App Engineer时出现的UnicodeDecodeError错误解决方案   ##问题描述 使用Python2.7.x运行GAE时有时会报这个错误 ```py ...

  5. c/c++常用代码 -- ini文件操作

    #pragma once #include <string> #include <sstream> typedef std::basic_string<TCHAR> ...

  6. [笔记]一个测试浏览器对html5支持的网站

    用需要测试的浏览器打开这个地址:http://html5test.com/

  7. [shell基础]——paste命令

    测试文本内容如下: # cat name1.txt name1 alvin1 name2 alvin2 name3 alvin3 name4 alvin4 # cat name2.txt name1 ...

  8. MDI窗体 的再度思考

    早在敲学生管理系统的时候,青山师哥就跟我说过  MDI 窗体 跟 子窗体的事情,当时只是简单的查询.小小的用了一下,没有太在意. 当再次面对着这个东西更深层的时候才想起师哥那句话.“好好研究一下这个东 ...

  9. Spark系列—01 Spark集群的安装

    一.概述 关于Spark是什么.为什么学习Spark等等,在这就不说了,直接看这个:http://spark.apache.org, 我就直接说一下Spark的一些优势: 1.快 与Hadoop的Ma ...

  10. 20145129 《Java程序设计》第4周学习总结

    20145129 <Java程序设计>第4周学习总结 教材学习内容总结 继承与多肽 继承共同行为 继承是避免多个类间重复定义共同行为.(将相同的代码提升为父类) 关键字extends:表示 ...