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 ...
随机推荐
- 【01】CSS3 Gradient 分为 linear-gradient(线性渐变)和 radial-gradient(径 向渐变)(转)
CSS3 Gradient 分为 linear-gradient(线性渐变)和 radial-gradient(径 向渐变).而我们今天主要是针对线性渐变来剖析其具体的用法.为了更好的应用 CSS3 ...
- codeforces 371B - Fox Dividing Cheese
#include<stdio.h> int count; int gcd(int a,int b) { if(b==0) return a; return gcd(b,a%b); ...
- [24点计算器][C++版本]无聊拿去玩
特性:数字数量.目标答案不限,当然数据大了会很慢... 基本可以去除所有本质相同的表达式...至少能等出结果的数据规模可以.. 安卓:http://yun.baidu.com/s/1slCGILn 程 ...
- JVM即时编译(JIT)
Java解释执行过程: 代码装入-代码校验-代码执行 Java字节码的执行方式分为两种:即使编译方式和解释执行方式.即时编译是值解释器先将字节码编译成机器码,然后执行该机器码.解释执行的方式是指解释器 ...
- AI小记-K近邻算法
K近邻算法和其他机器学习模型比,有个特点:即非参数化的局部模型. 其他机器学习模型一般都是基于训练数据,得出一般性知识,这些知识的表现是一个全局性模型的结构和参数.模型你和好了后,不再依赖训练数据,直 ...
- Animations动画和Keyframes关键帧
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- python 类、模块、包的区别
学习python的时候,碰到了import 和 from xx import xx的问题, 为了弄清楚什么是 module 和package ,这篇文章讲解的不错!! 原文: http://www.c ...
- jsp,velocity,freemark页面引擎的比較
在java领域.表现层技术主要有三种:jsp.freemarker.velocity. jsp是大家最熟悉的技术 长处: 1.功能强大,能够写java代码 2.支持jsp标签(jsp tag) 3.支 ...
- 【Android】资源系列(一) -- 国际化(多语言)
1.Android 中要实现国际化比較简单. 字符串国际化:仅仅要在 res 目录下新建相应语言的 values 目录就好了. 如.英语环境下的.目录命名为:values-en ...
- Datazen自己定义地图
Datazen的地图数据定义主要以ESRI的Shape文件格式为主,这是现现在被广泛使用的一种地图数据格式.在Datazen中,自己定义地图须要提供例如以下两个地图数据定义文件: SHP文件提供 ...