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的配置项,既然存在拓展方法,又遇到需求,那么就 ...
随机推荐
- poj 2377 Bad Cowtractors
题目连接 http://poj.org/problem?id=2377 Bad Cowtractors Description Bessie has been hired to build a che ...
- C++ json库jsoncpp 吐槽
Explain 最近在做游戏接入SDK时用到C++的json库jsoncpp,jsoncpp 是一款优秀的json库,但恶心的一点是它采用Assert作为错误处理方法,而assert在linux下通过 ...
- R 语言中文乱码问题
R 语言似乎在WINDOWS平台上对中文的支持不是特别好,似乎是3.1.2的一个BUG. 目前我研究出了一个临时解决方案,你可以将代码编写成一个函数,从而在调用的过程中不必如下繁琐: 1. 先将本地语 ...
- WPF——数据绑定(一)什么是数据绑定
注意:本人初学WPF,文中可能有表达或者技术性问题,欢迎指正!谢谢! 一:什么是数据绑定? “Windows Presentation Foundation (WPF) 数据绑定为应用程序提供了一种简 ...
- 10、android学习资源整理
1.github上整理好的开源工程 https://github.com/Trinea/android-open-project 2.最流行的android组件大全 http://colobu.com ...
- 【Sum Root to Leaf Numbers】cpp
题目: Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a nu ...
- java list中的对象,按对象某属性排序
1:对象类 需要 实现: public class TreeNode extends BaseBean implements Comparable <TreeNode> { private ...
- git的初步了解
其实git我也不熟,请两天假回来宿友告诉我,我们有一份高大尚的作业.我问她们才知道原来是让我们以博客的形式写两份作业交上去.git还是我在网上查找到的,才对它有一些的了解. git是一个开源分布式版本 ...
- discuz之同步登入
前言 首先感谢dozer学长吧UCenter翻译成C# 博客地址----------->http://www.dozer.cc/ 其次感谢群友快乐々止境同学的热心指导,虽然萍水相逢但让我 ...
- C++ Template之函数模版
函数模版的定义: template <typename T> T const& max(const T& a,const T b) { return a > b ? ...