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(, , , );
}
- (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多选全选的更多相关文章
- Dynamic CRM 2013学习笔记(二十六)报表设计:Reporting Service报表 动态参数、参数多选全选、动态列、动态显示行字体颜色
上次介绍过CRM里开始报表的一些注意事项:Dynamic CRM 2013学习笔记(十五)报表入门.开发工具及注意事项,本文继续介绍报表里的一些动态效果:动态显示参数,参数是从数据库里查询出来的:参数 ...
- 基于JQ的多选/全选/反选及获取选中的值
<!-- author:青芒 --> <!DOCTYPE html> <html lang="en"> <head> <met ...
- Jquery 多选全选/取消 选项卡切换 获取选中的值
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- vue-element 动态单选多选全选
实现效果如图 数据格式如下: pps: [{"code":"6","createTime":"2018-09-07 00:00:0 ...
- checkout 多选 全选(亲测有效)
<input type="button" id="btn1" value="全选"> <input type=" ...
- js分类多选全选
效果如图: HTML代码: <div class="form-group quanxian-wrap"> <label>项目</label> & ...
- wpf中为DataGrid添加checkbox支持多选全选
项目中用到DataGrid, 需要在第一列添加checkbox, 可以多选.全选. 其中涉及的概念DataTemplate, DataGridCellStyle, DataGridCellContro ...
- layui 复选框checkbox 实现全选全选
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Antd组件Table树型多选全选问题
组件库antd里面的树型选择不能做到勾选父组件然后一起勾选子组件情况,我也不知道是组件库的问题还是原本设计就是这样 刚好组件库存在rowselection的配置项,既然存在拓展方法,又遇到需求,那么就 ...
随机推荐
- 手动书写小代码-foreach实现机制
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.C ...
- 编译原理之lex,yacc学习
写在前面的几句废话 最近在项目的过程中接触了lex 和 yacc,他们可以帮助我们来实现自己的领域语言.最典型的应用就是可以帮助我们来实现自定义测试脚本的执行器.但是,这里也有一个限制,就是测试脚本要 ...
- eclipse+cdt+minGW (C/C++ 编译)
1. 安装Eclipse CDT 方法1: 已安装Eclipse的话,可以通过菜单Help->Install New Software,安装CDT插件. 点击ADD后 Name:CDT L ...
- Go support for Android
Go support for Android David Crawshaw June 2014 Abstract We propose to introduce Go support for the ...
- JSP的7个动作指令
动作指令与编译指令不同,编译指令是通知Servlet引擎的处理信息,而动作指令知识运行时的动作.编译指令在将JSP编译成Servlet时起作用,而处理指令通常可替换成JSP脚本,它知识JSP脚本的标准 ...
- [shell基础]——awk命令
关于awk awk是一个强大的文本分析工具,相对于grep的查找.sed的编辑,awk在其对数据分析并生成报告时,显得尤为强大. 简单来说awk就是把文件逐行的读入,以空格为默认分隔符将每行切片,切开 ...
- 团队开发——SCRUM报告(一)
一.成员介绍 队长:胡亚宝 PM:曹美娜 成员:焦燕.袁亚姣.黄亚萍 二.sprint会议 由于之前是一五一小长假,所以距离上次会议中间隔了很长时间,这里在对上次会议做一下简单的汇总 在上次会议上我们 ...
- 再论 ASP.NET 中获取客户端IP地址
说到IP获取无非是我们常见的以下几种方式,但是具体获取的值具体区别在哪?网上不乏相关文章,说的也是很详细,但是真正使用起来,还有很多不太对的地方.IP在不同系统中,应用相当广泛,常见的日志记录.广告分 ...
- 网站中的专题页或者tag聚合页的权重不错
一.据最近的一些观察,觉得网站中的专题页或者tag聚合页的权重不错,因此多给网站制作一些专题页面,不仅有利于聚合站内的文章,更是绝对的原创内容,应该会受到百度的青睐.简评:关于权重的讨论,这篇无疑是很 ...
- android 自动化压力测试-monkey 1 实践
Monkey是Android中的一个命令行工具,可以运行在模拟器里或实际设备中.它向系统发送伪随机的用户事件流(如按键输入.触摸屏输入.手势输入等),实现对正在开发的应用程序进行压力测试.Monkey ...