思路1

一、单选

1、创建一个currentIndexPath为了记录当前选择的indexPath。
2、 如果是记录上一次选中的内容,可以通过模型中的是否选中来初始化indexPathForRow。
3、首先判断创建的indexPath是否等于当前点击的indexPath。
4、如果不等于数组中的模型全部置为NO。
5、再将当前点击的indexPath传给创建的currentIndexPath,并刷新当前表格(传创建的currentIndexPath)。
6、通过点击的indexPath取出当前模型并取反,再通过点击的indexPath刷新表格
代码如下:
1、_indexPath = [NSIndexPath indexPathForRow:[group.brand indexOfObject:viewModel] inSection:[_dataArray indexOfObject:group]];//取出选中的行及列,viewModel表示是选中的模型,如果进入页面不需要设置上一次选中的内容可以不写这一步。
2、NSIndexPath *selectIndexPath = _indexPath;
if (selectIndexPath && selectIndexPath != indexPath) {//判断创建的indexPath是否不等于点击的indexPath
for (GMeSaleAreaGroupViewModel *GviewModel in _dataArray) {//如果不等于置为NO
for (GMeSaleAreaViewModel*viewModel in GviewModel.brand) {
viewModel.isSelect = NO;
}
}
//刷新没有选中的表格
[tableView reloadRowsAtIndexPaths:@[_indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
_indexPath = indexPath; //点击的indexPath传给上一个点击的indexPath
GMeSaleAreaViewModel *viewModel = [GroupViewModel.brand objectAtIndex:indexPath.row]; //取出当前点击的indexPath
viewModel.isSelect = !viewModel.isSelect;//取反
[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];//刷新当前点击的表格

  

二、全选

  1.创建可变数组,存储所有未选中状态(NO)的布尔值按钮,点击时改变其状态,并传入按钮的状态。

三、多选

  1.创建Cell时,从数组中取出相应的值,传给cell,如果为YES,否则为NO.

  2.点击cell时,从数组中取出相应的值,取反,然后刷新该行。

四、代码先行

#import "ViewController.h"
#import "CheckBoxCell.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>{ UITableView *_tableView;
}
@property (nonatomic, strong)NSMutableArray *dataArray;
@end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self loadUI];
[self loadDataWithSelected:NO];
}
- (void)loadUI{ _tableView = [[UITableView alloc] initWithFrame:CGRectMake(, ,self.view.bounds.size.width,self.view.bounds.size.height - ) style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
[self.view addSubview:_tableView]; UIButton *customBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[customBtn setFrame:CGRectMake(, , , )];
[customBtn setBackgroundColor:[UIColor lightGrayColor]];
[customBtn setTitle:@"多选" forState:UIControlStateNormal];
[customBtn setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
[customBtn setTitle:@"全选" forState:UIControlStateSelected];
[customBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:customBtn];
}
- (void)loadDataWithSelected:(BOOL)isSelected{ NSMutableArray *arr = [NSMutableArray array];
for (NSInteger i = ; i< ; i++) {
[arr addObject:@(isSelected)];
}
self.dataArray = arr;
[_tableView reloadData];
}
- (void)btnClick:(UIButton *)sender{ [self loadDataWithSelected:sender.selected];
sender.selected = !sender.selected; }
#pragma mark --- tableview
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *ID = @"checkBoxCell";
CheckBoxCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (cell == nil) {
cell = [[CheckBoxCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
}
if (self.dataArray.count > indexPath.row) {
NSNumber *selected = [self.dataArray objectAtIndex:indexPath.row];
[cell setChecked:selected.boolValue];
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ [tableView deselectRowAtIndexPath:indexPath animated:YES]; if (self.dataArray.count > indexPath.row) {
NSNumber *selected = self.dataArray [indexPath.row];
self.dataArray[indexPath.row] = @(!selected.boolValue);
[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
} }

cell代码同下;

思路2

原文地址: http://www.cnblogs.com/hxwj/p/4532172.html

思路分析

前期准备:1.创建两个数组:一个数据数组array,一个状态数组checkArray,(初始化时每一个值为NO).

     2.多选,初始化时存入所有NO值给checkArray,点击cell时取出每一值取反,全选则是根据按钮状态,如果是选中全部为YES,否则全部为NO。

一.全选

  1.点击时反选按钮状态,取出所有indexPath未选中状态给新的数组,然后遍历取出相应的indexpath,cell和row;

2.取出每一个值,如果是选中,设置为YES,否则设置为NO.

二.多选(点击cell时,取出相应的row和cell,根据row从checkArray取出一项,并设置cell状态)

  1.当cell点击时,根据点击cell的row从checkArray取出相应的项,改变其值,如果是YES,设置为YES,否则设置为NO。

三.代码先行

cell

#import "CheckBoxCell.h"
@interface CheckBoxCell(){ BOOL _isSelect;
UIImageView *_chectImageView; }
@end
@implementation CheckBoxCell - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
if (self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]) { _chectImageView = [[UIImageView alloc] init];
_chectImageView.image = [UIImage imageNamed:@"normal"];
[self.contentView addSubview:_chectImageView];
}
return self;
} - (void)layoutSubviews{ [super layoutSubviews];
[_chectImageView setFrame:CGRectMake(, ,, )];
}
- (void)setchecked:(BOOL)checked{ _isSelect = checked;
if(_isSelect){ _chectImageView.image = [UIImage imageNamed:@"select"];
}else{
_chectImageView.image = [UIImage imageNamed:@"normal"];
}

VIewControll

#import "ChectBoxViewController.h"
#import "CheckBoxCell.h"
@interface ChectBoxViewController ()<UITableViewDelegate,UITableViewDataSource>{ UITableView *_tableView;
}
@property (nonatomic, strong)NSArray *array;
@property (nonatomic, strong)NSMutableArray *chectArray;
@end @implementation ChectBoxViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self loadUI];
[self loadData];
}
#pragma mark --- viewDidLoad
- (void)loadUI{ _tableView = [[UITableView alloc] initWithFrame:CGRectMake(,, self.view.bounds.size.width, self.view.bounds.size.height) style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
[_tableView setBackgroundColor:[UIColor lightGrayColor]];
[self.view addSubview:_tableView]; UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setTitle:@"多选" forState:UIControlStateNormal];
[btn setTitle:@"全选" forState:UIControlStateSelected];
[btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
[btn setFrame:CGRectMake(, , , )];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[self.view addSubview:btn];
}
- (void)loadData{ _chectArray = [NSMutableArray array];
for (NSInteger i = ; i < self.array.count; i++) {
NSMutableDictionary *dic = [NSMutableDictionary dictionary];
[dic setValue:@"NO" forKey:@"checked"];
[_chectArray addObject:dic];
}
}
#pragma mark --- tableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.array.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *ID = @"checkboxCell";
CheckBoxCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (cell == nil) {
cell = [[CheckBoxCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ [tableView deselectRowAtIndexPath:indexPath animated:YES]; CheckBoxCell*cell = (CheckBoxCell *)[tableView cellForRowAtIndexPath:indexPath];
NSInteger row = indexPath.row;
[self cellChecked:cell row:row isSelected:YES];
} #pragma mark --- response methods
/**
* 点击,和加载cell的时候进行判断,从而改变cell的选中状态
*
* @param cell 自定义cell
* @param row tableView的下标
* @param selected 是否是点击
*/
- (void)cellChecked:(CheckBoxCell *)cell row:(NSInteger)row isSelected:(BOOL)selected{ NSMutableDictionary *dic = [_chectArray objectAtIndex:row];
if ([[dic objectForKey:@"checked"] isEqualToString:@"NO"]) {
if (selected) {
[dic setObject:@"YES" forKey:@"checked"];
[cell setchecked:YES];
}
}else{ if (selected) {
[dic setObject:@"NO" forKey:@"checked"];
[cell setchecked:NO];
}
}
}
- (void)btnClick:(UIButton *)sender{//全选 sender.selected = !sender.selected; NSArray *arrayOfIndexPath = [NSArray arrayWithArray:[_tableView indexPathsForVisibleRows]];
for (NSInteger i = ; i < arrayOfIndexPath.count; i++) {
NSIndexPath *indexPath = [arrayOfIndexPath objectAtIndex: i];
CheckBoxCell *cell = (CheckBoxCell *)[_tableView cellForRowAtIndexPath:indexPath];
NSUInteger row = [indexPath row];
NSMutableDictionary *dic = [_chectArray objectAtIndex:row];
if (sender.selected) {
[dic setObject:@"YES" forKey:@"checked"];
[cell setchecked:YES];
}else{
[dic setObject:@"NO" forKey:@"checked"];
[cell setchecked:NO];
}
}
}
- (NSArray *)array{
if (_array == nil) {
_array = [[NSMutableArray alloc] initWithObjects:@"",@"",@"",@"",@"", nil];
}
return _array;
}

UITableViewCell 多选和全选(checkBoxCell)的更多相关文章

  1. IOS开发学习笔记029-反选、全选、删除按钮的实现

    还是在上一个程序的基础上进行修改 1.反选按钮 2.全选按钮 3.删除按钮 4.其他代码优化 1.反选按钮 反选的过程就是将_deleteShops数组中得数据清空,然后将Shops中数组添加到_de ...

  2. js 复选框 全选都选 如果某一个子复选框没选中 则全选按钮不选中

    <!DOCTYPE HTML> <html> <head> <meta charset=UTF-8> <title>js 复选框 全选都选 ...

  3. CheckBox复选框全选以及获取值

    <html> <head> <meta http-equiv="Content-Type" content="text/html; char ...

  4. JavaScript小例子:复选框全选

    JavaScript小例子:复选框全选 这只是一个小例子,很简单,但是这个功能还是很常用的: 实现后效果如图: JavaScript代码: <script type="text/jav ...

  5. Jquery表格变色 复选框全选,反选

    /*jquery静态表格变色*/ $(".tr2").mouseover(function(){ $(this).css("background"," ...

  6. 复选框全选、全不选和反选的效果实现VIEW:1592

    <html> <head> <meta http-equiv="Content-Type" content="text/html; char ...

  7. Jquery CheckBox复选框 全选/取消全选 最佳实现方式 参考案例

    <input id="chkAll" type="checkbox" />全选/取消全选</div> <asp:Repeater ...

  8. html+css+js实现复选框全选与反选

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  9. checkbox复选框全选批量删除

    多选框全选实现批量删除 html代码 <body> <form action="" method="post" name="Form ...

  10. 关于在repeater中的checkbox实行多选和全选

    今天项目中用到这一块,是一个b2b商城,业务是别人给客户留言后,客户从会员中心的留言管理中查看,用checkbox实行多选和全选后进行批量审核 首先在checkbox后加个hidden,作用见代码: ...

随机推荐

  1. 之二:CAKeyframeAnimation - 关键帧动画

    是CApropertyAnimation的子类,跟CABasicAnimation的区别是:CABasicAnimation只能从一个数值(fromValue)变到另一个数值(toValue),而CA ...

  2. 微信小程序开发之如何哪获取微信小程序的APP ID

    微信小程序的开发工具,在新建项目的时候,默认提示填写APP ID,如果不填写AppID 也是可以本地测试和开发的,但是无法通过手机调试,只能在开发工具里查看 如果需要真机调试微信小程序,需要安装微信6 ...

  3. 福利->KVC+Runtime获取类/对象的属性/成员变量/方法/协议并实现字典转模型

    我们知道,KVC+Runtime可以做非常多的事情.有了这个,我们可以实现很多的效果. 这里来个福利,利用KVC+Runtime获取类/对象的所有成员变量.属性.方法及协议: 并利用它来实现字典转模型 ...

  4. RxJava 和 RxAndroid 二(操作符的使用)

    前言:对Rx不了解的朋友可以先看我的第一篇博文 RxJava 和 RxAndroid 一 (基础),是对Rxjava的基本介绍 1.merge操作符,合并观察对象 List<String> ...

  5. 当SD卡拔出时,返回首页,栈中的activity都要清除,只留下首页的activity

    目标:当SD卡拔出时,返回首页,栈中的activity都要清楚,只留下首页的activity 我在清单中注册了一个静态广播: <receiver android:name="com.p ...

  6. 【CoreData】表之间的关联

    这次是表之间怎么进行关联,要求如下: // 建立学生与班级表之间的联系 既然是表与表之间的关联,那肯定是要先创建表: // 1.创建模型文件 (相当于一个数据库里的表) // New File ——— ...

  7. TCP/IP 协议难点之一—— IP分片

    1 IP协议简单介绍 就个人而言,网络中,抛开网络安全加密这些,就只单单讨论协议本身,比较难的有三个地方: IP分片与重组 TCP滑动窗口与TCP状态的改变 TCP定时器 其实协议本身根据<TC ...

  8. iOS iOS9.0 的CoreLocation定位

    一.简介 iOS9.0如果当前处于前台授权状态,默认是不可以后台获取用户位置. 如果在前台授权下,让其能获取到后台定位,该怎么办 可以设置以下属性为YES,就可以继续获取后台位置,但是会出现蓝条 使用 ...

  9. ASP.NET MVC Bootstrap极速开发框架

    前言 每次新开发项目都要从头开始设计?有木有一个通用的快速开发框架?并且得是ASP.NET MVC  And Bootstrap?数据库不要手工创建?框架对未来业务支持的扩展性好?这么简单的功能还需要 ...

  10. php5.3新特性 之 mysql native driver(mysqlnd)

    概述 本文主要写给sa看的.码农就不用看了. mysql native driver(mysqlnd) 自从php5.3.0开始成为官方源代码的一部分, 用来取代传统的mysql client lib ...