iOS - UITableView 多选功能实现
:自定义Cell中的代码 #import <UIKit/UIKit.h>
@interface TestCell : UITableViewCell
@property(nonatomic,copy)NSString *content;
/**
* 标记行是否被选中
*/
@property(nonatomic,assign)BOOL isChecked;
@end
#import "TestCell.h"
@interface TestCell()
@property (weak, nonatomic) IBOutlet UIImageView *mark_ImageView;
@property (weak, nonatomic) IBOutlet UILabel *firstLabel; @end @implementation TestCell - (void)awakeFromNib
{
self.mark_ImageView.hidden = YES;
} - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated]; }
-(void)setContent:(NSString *)content
{
if (_content!=content) {
_firstLabel.text=content;
} }
/**
* 这里只是用到了一张图所以通过来imageview的hidden来显示是否选中
*
* @param isChecked
*/
- (void)setIsChecked:(BOOL)isChecked
{
_isChecked = isChecked;
//选中
if (_isChecked) {
self.mark_ImageView.hidden = NO;
self.mark_ImageView.image = [UIImage imageNamed:@"choice"];
}
else
{
self.mark_ImageView.hidden = YES;
} } @end :ViewController中的代码 #import "ViewController.h"
#import "TestCell.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate> @property(nonatomic,strong)NSMutableArray *dataArray; /**
* 保存选中行的对应的数据
*/
@property(nonatomic,strong)NSMutableArray *markArray; /**
* 保存标记选中行字典的数组
*/
@property(nonatomic,strong)NSMutableArray *checkArray; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; for (NSInteger i=; i<; i++) {
[self.dataArray addObject:[NSString stringWithFormat:@"选中第%ld行",i]];
} for (NSInteger i =; i<self.dataArray.count; i++) {
NSMutableDictionary *resultDict = [NSMutableDictionary dictionaryWithObject:@ forKey:@"isChecked"];
[self.markArray addObject:resultDict];
} } - (NSMutableArray *)dataArray{ if (!_dataArray) {
_dataArray =[NSMutableArray array];
}
return _dataArray; } - (NSMutableArray *)markArray{ if (!_markArray) {
_markArray =[NSMutableArray array];
}
return _markArray; } - (NSMutableArray *)checkArray{ if (!_checkArray) {
_checkArray =[NSMutableArray array];
}
return _checkArray; } -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return ;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.dataArray.count;
}
static NSString *cellIdentifier = @"TestCell";
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{ TestCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
cell.content= self.dataArray[indexPath.row];
NSDictionary *resultDict = self.markArray[indexPath.row];
cell.isChecked = [resultDict[@"isChecked"] boolValue];
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 44.0f;
} - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
TestCell *cell = (TestCell *)[tableView cellForRowAtIndexPath:indexPath];
NSString *checkString = self.dataArray[indexPath.row];
NSMutableDictionary *checkedResult = self.markArray[indexPath.row];
//isChecked为NO则表明要把这行置为选中状态
if ([checkedResult[@"isChecked"] boolValue]==NO) {
[checkedResult setValue:@ forKey:@"isChecked"];
cell.isChecked = YES;
[self.checkArray addObject:checkString]; }
else{
[checkedResult setValue:@ forKey:@"isChecked"];
cell.isChecked = NO;
[self.checkArray removeObject:checkString]; }
[tableView deselectRowAtIndexPath:indexPath animated:YES];
//刷新指定的行
NSIndexPath *indexPath_Row=[NSIndexPath indexPathForRow:indexPath.row inSection:];
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath_Row,nil] withRowAnimation:UITableViewRowAnimationNone];
NSLog(@"self.checkArray =%@",self.checkArray); } @end

iOS - UITableView 多选功能实现的更多相关文章
- IOS UITableView多选删除功能
UITbableView作为列表展示信息,除了展示的功能,有时还会用到删除,比如购物车.收藏列表等. 单行删除功能可以直接使用系统自带的删除功能,当横向轻扫cell时,右侧出现红色的删除按钮,点击删除 ...
- IOS UITableView拖动排序功能
UITbableView作为列表展示信息,除了展示的功能,有时还会用到删除,排序等功能,下面就来讲解一下如何实现排序. 排序是当表格进入编辑状态后,在单元格的右侧会出现一个按钮,点击按钮,就可以拖动单 ...
- ios UITableView多选删除
第一步, - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath ...
- IOS UITableView索引排序功能
UITbableView分组展示信息时,有时在右侧会带索引,右侧的索引一般为分组的首字母,比如城市列表的展示.当点击右侧索引的字母,列表会快速跳到索引对应的分组,方便我们快速查找.下面,就介绍一下索引 ...
- iOS开发——UI_swift篇&UITableView实现索引功能
UITableView实现索引功能 关于UItableView的索引在平时项目中所见不多,最多的就是跟联系人有关的界面,虽然如此,但是作为一个swift开发的程序必须知道的一个技术点,所以今天 ...
- iOS中的界面多选功能--(UICollectionView)
文/Jacob_Pan(简书作者)原文链接:http://www.jianshu.com/p/9d28ebd0f5a2著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”. 最近做项目接触了一 ...
- iOS UITableView的多选
一些列表经常需要编辑多选的功能,而UITableview自带多选删除的功能,使用起来方便,不需要自己去做数据存储和选中状态转换,可以减少不少开发时间.下面就来介绍下UITableView多选的使用. ...
- iOS UITableView划动删除的实现
标签:划动删除 iphone 滑动删除 ios UITableView 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://rainb ...
- UITableView多选删除
设置一个在编辑状态下点击可改变图片的cell FileItemTableCell.h #import <UIKit/UIKit.h> @interface FileItemTableCel ...
随机推荐
- 【CV论文阅读】Dynamic image networks for action recognition
论文的重点在于后面approximation部分. 在<Rank Pooling>的论文中提到,可以通过训练RankSVM获得参数向量d,来作为视频帧序列的representation.而 ...
- jQuery -> 获取/设置HTML或TEXT内容
jQuery提供了两个API能够直接用来为元素加入内容. html() text() 当中html()是为指定的元素加入html内容 text()是为指定的元素加入文本内容 两者的差别在于,text中 ...
- 导出excel——弹出框
表单提交 凡是表单提交(表单提交分3种,见以下的1.2.3)的话.而且设置了表单标签的enctype="multipart/form-data"属性.那么这个时候就会打开弹出框. ...
- 《Linux Device Drivers》第八章 分配内存——note
本章主要介绍Linux内核的内存管理. kmalloc函数的内幕 不正确所获取的内存空间清零 分配的区域在物理内存中也是连续的 flags參数 <linux/slab.h> <lin ...
- iOS中的成员变量,实例变量,属性变量
在ios第一版中: 我们为输出口同时声明了属性和底层实例变量,那时,属性是oc语言的一个新的机制,并且要求你必须声明与之对应的实例变量,例如: 注意:(这个是以前的用法) @interface MyV ...
- UVA 23 out of 5
题目例如以下: Problem I 23 Out of 5 Input: standard input Output: standardoutput Time Limit: 1 second Memo ...
- CentOS-6.4-DVD系统中安装Oracle-11.2.0.4
完整版见https://jadyer.github.io/2014/05/18/centos-install-oracle/ /** * CentOS-6.4-DVD系统中安装Oracle-11.2. ...
- FOBiz组合模糊查询
List list= delegator.findList("Entity",condition , null, null, null, false);其中condition为:组 ...
- 如何利用UDP协议封装一个数据包
在如何封装一个数据包上,是一个非常细致的问题,而利用UDP协议来封装的话,是比较简单,让我们一步步来分析典型的TCP/IP协议.一般来说一个典型的一个数据包,包括以太网MAC头+网络层IP数据头+传输 ...
- 推荐微软Windows 8 Metro应用开发虚拟实验室
Kevin Fan分享开发经验,记录开发点滴 推荐微软Windows 8 Metro应用开发虚拟实验室 2012-07-19 05:23 by jv9, 1940 阅读, 4 评论, 收藏, 编辑 微 ...