自定义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. bat完美关机命令

    @echo off title OLIVER-COMPUTER mode con cols= lines= color 0d shutdown /a >nul >nul ver > ...

  2. OpenStack:安装Glance

    >安装Glance1. 安装# apt-get install glance python-glanceclient删除sqlite文件rm -f /var/lib/glance/glance. ...

  3. web relase

    http://wenku.baidu.com/link?url=uOAV9QwXGGLjeqt6M1KTqwp0Jbhhguvz9IxExCHNiUlrYMX584Io3ByNJJIkAVzEqzv9 ...

  4. 嗨分享-前端技术-帝国CMS手机站修改列表分页(sysShowListMorePage)

    http://bbs.phome.net/showthread-31-318753-0.html 如果你的网站使用的是帝国CMS.PC站和手机站各使用一个模板组,但共同使用一个数据库.那么你的PC站和 ...

  5. [转]Ubuntu中root用户和user用户的相互切换

    [转]Ubuntu中root用户和user用户的相互切换 http://www.cnblogs.com/weiweiqiao99/archive/2010/11/10/1873761.html Ubu ...

  6. Qt 延时

    第一部分: 关于sleep函数,我们先来看一下他的作用:sleep函数是使调用sleep函数的线程休眠,线程主动放弃时间片.当经过指定的时间间隔后,再启动线程,继续执行代码.sleep函数并不能起到定 ...

  7. 零基础学习Linux(一)环境搭建

    从本文开始我会为大家介绍一下linux环境下详细的集群环境安装.配置.部署到实例演示的整个过程.在此过程中会给大家详细介绍一下Linux的操作技巧和一些工具的使用.今天开始第一步——环境搭建. 第一步 ...

  8. Hibernate从入门到精通(十)多对多单向关联映射

    上一篇文章Hibernate从入门到精通(九)一对多双向关联映射中我们讲解了一下关于一对多关联映射的相关内容,这次我们继续多对多单向关联映射. 多对多单向关联映射 在讲解多对多单向关联映射之前,首先看 ...

  9. spring IOC源码分析(1)

    1.何谓Spring IOC 何谓Spring IOC?书上谓之“依赖注入”,那何谓“依赖注入”? 作为一个Java程序猿,应该遇到过这样的问题,当你在代码中需要使用某个类提供的功能时,你首先需要ne ...

  10. unity工具IGamesTools之批量生成帧动画

    unity工具IGamesTools批量生成帧动画,可批量的将指定文件夹下的帧动画图片自动生成对应的资源文件(Animation,AnimationController,Prefabs) unity工 ...