自定义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中的排序

    排序是数据结构中重要的一个部分,也是在实际开发中最易遇到的问题之一,当然了,你也可以不考虑这些排序的算法,直接把要排序的数据insert到数据库中,用数据库的order by再select一下,也能产 ...

  2. Oracle出现字符集问题处理方法

    1.  Cmd进去DOS 2.  再输入dbca(database  create) 3.  弹出的界面,直接下一步,选择删除数据库 4.  成功删除后,回到一第一界面,选择创建数据库,下一步. 5. ...

  3. Tomcat 服务器服务的注册修改删除

    1. 注册Tomcat服务 运行cmd,切换目录到tomcat/bin, 执行以下命令service.bat install 2.删除Tomcat服务

  4. 6.24 AppCan移动开发者大会,我爱我家即将闪亮登场!

    6.24 AppCan移动开发者大会进入倒计时,报名通道即将关闭! “6月24日, 2016AppCan移动开发者大会即将召开,以“平台之上,应用无限”为主题,1500位行业精英汇聚在此,重磅新品发布 ...

  5. 【Javascript】: for循环中定义的变量在for循环体外也有效

    for循环中定义的变量在for循环体外也有效 <script> (function(){ var a = 111; for(var i=0;i<5;i++){ var carl = ...

  6. 用Java实现3DES

    3DES,即三重DES,是DES的加强版,也是DES的一个更安全的变形.它使用3个56位(共168位)的密钥对数据进行三次加密,和DES相比,安全性得到了较大的提高. 实际上,3DES是一个过渡的加密 ...

  7. ExtJS MVC 学习手记3

    在演示应用中,我们已经创建好了viewport,并为之添加了一个菜单树.但也仅仅是这样,点击树或应用的其他地方获得不到任何响应.这个演示应用还是一个死的应用. 接下来,我们让这个应用活起来. 首先,给 ...

  8. Java实现Socket之TimeClient

    Java实现Socket之TimeClient 代码内容 从time.nist.gov服务器的37号端口得到时间信息,并对时间进行解析后显示出来 代码实现 /* TimeClient.java */ ...

  9. 在EDK里面添加ISE IP core的方法

    (1)在ISE下,使用core generator,可以得到xilinx的IP的*.v和*.ngc 文件,将这两个文件拷贝出来: (2)在EDK下使用“Create or Import Periphe ...

  10. MATLAB中匿名函数与符号函数的转换

    符号函数举例: syms x y=x+1; y1=diff(y); %求导 匿名函数举例: z=@(x)(x(1)+2*x(2)); t=z([2 3]); %计算z在x=[2 3]处的值 z=@(x ...