ios开发之--tableview单选/多选实现(非tableview的editing状态)及默认选中
实现思路比较简单,这里仅做记录:
直接上代码:
1,实现didSelectRowAtIndexPath方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[[NSUserDefaults standardUserDefaults]setValue:[array objectAtIndex:indexPath.row] forKey:APP_CHANGEVOICE]; [_sextTableView reloadData];
[tableView deselectRowAtIndexPath:indexPath animated:YES]; }
在cellForRowAtIndexPath里面实现方法
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellTableIdentifier = @"CellTableIdentifier";
hPublickCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell == nil)
{
cell = [[hPublickCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellTableIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
} cell.textsLabel.text = array[indexPath.row];
cell.selectionStyle=UITableViewCellSelectionStyleGray;
//选择状态的存储
if ([[[NSUserDefaults standardUserDefaults]valueForKey:APP_CHANGEVOICE] isEqualToString:[array objectAtIndex:indexPath.row]])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
} return cell; }
这里面的array是数据源数组。效果图如下:

2,上面这种是系统的选中样式,下面是自定义的:
代码如下:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
ShippingAddressCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ShippingAddressCell"];
if (!cell) {
cell = [[ShippingAddressCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ShippingAddressCell"];
} if (self.lastIndexPath == indexPath)
{
cell.selectedImg.image = [UIImage imageNamed:@"clicked"];
}else
{
cell.selectedImg.image = [UIImage imageNamed:@"unClick"];
} return cell;
} -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//之前选中的,取消选择
ShippingAddressCell *celled = [tableView cellForRowAtIndexPath:_lastIndexPath];
celled.selectedImg.image = [UIImage imageNamed:@"unClick"];
//记录当前选中的位置索引
_lastIndexPath = indexPath;
//当前选择的打勾
ShippingAddressCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.selectedImg.image = [UIImage imageNamed:@"clicked"];
}
这样就可以实现了,截图如下:

多选的有空再完善!代码可以直接粘贴使用!
多选,这里我这个单选,没有使用编辑模式,很简单,在这里记录下,demo就不附了,我把代码都粘出来,
我的tableview是拖的xib,不想写,代码如下:
1,自定义cell,我是在最左边有一个imageView,如下图:

2,相关代码:
a 声明
@property (weak, nonatomic) IBOutlet UITableView *startTableView;
@property (strong, nonatomic) NSMutableArray *selectIndexs; //多选选中的行
b viewDidLoad里面
_selectIndexs = [[NSMutableArray alloc]initWithCapacity:];
for (int i = ; i < dataAry.count; i ++) {
[_selectIndexs addObject:@""];
}
c tableview的相关代理方法:
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return dataAry.count;
} -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
StartReachedCell *cell = [tableView dequeueReusableCellWithIdentifier:@"StartReachedCell"];
if (!cell) {
cell = [[StartReachedCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"StartReachedCell"];
} cell.descLab.text = dataAry[indexPath.row]; if ([_selectIndexs[indexPath.row] isEqualToString:@""]) {
cell.selImg.image = [UIImage imageNamed:@"unsel"];
}else
{
cell.selImg.image = [UIImage imageNamed:@"sel"];
cell.tintColor = BLUECOLOR;
} return cell;
} -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
StartReachedCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSString *stateStr = _selectIndexs[indexPath.row]; //连续点击的时候,两种状态进行切换
NSString *picName = [stateStr isEqualToString:@""]? @"sel":@"unsel";
NSString *changeStateStr = [stateStr isEqualToString:@""]?@"":@"";
cell.selImg.image = [UIImage imageNamed:picName];
[_selectIndexs replaceObjectAtIndex:indexPath.row withObject:changeStateStr];
}
效果如下图:

基本的逻辑是,声明一个空的数组,里面存放我自定义的状态:"0"和"1",第一次进去的时候,是没有状态1的,只有0,在cell的初始化方法里面直接判断展示即可,在进行过点击操作后,在didSelectRowAtIndexPath方法里面把选中所产生的新状态替换掉原来数组里面的老状态,一次循环展示即可。如果想要下次进去是上一次默认的选中,直接把数组存起来即可。
网上有很多的编辑状态,确实很好用,也很简单,有空我会更新下,把单选、复选、多选,全部都更新出来,我这种短小精悍。大家有什么好的写法,可以留言!
默认选中:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.tableView selectRowAtIndexPath:self.lastIndexPath animated:YES scrollPosition:UITableViewScrollPositionTop];
NSIndexPath *path = [NSIndexPath indexPathForItem: inSection:];
[self tableView:self.tableView didSelectRowAtIndexPath:path];
}
ios开发之--tableview单选/多选实现(非tableview的editing状态)及默认选中的更多相关文章
- iOS开发之--storyboary下,拖拽一个tableview/collectionView/view 等,顶端下沉64个像素的处理方法
大家可能会发现,在sb或者xib里面拖拽一个tableview/collectionview/view的,顶端会自动下沉64个像素,也就是说,运行在模拟器上去,自导航下面又自动下沉了64个像素, 那是 ...
- 【iOS开发-68】APP下载案例:利用tableView自带的cell布局+缓存池cell复用时注意button状态的检查
(1)效果 (2)源码与资源下载 http://pan.baidu.com/s/1pJLo2PP (3)总结 --核心是利用UITableView里面自带的cell来制作样式同样的cell. 与之对应 ...
- iOS开发——网络篇——数据安全(MD5),HTTPS,检测网络状态
一.数据安全 1.提交用户的隐私数据一定要使用POST请求提交用户的隐私数据GET请求的所有参数都直接暴露在URL中请求的URL一般会记录在服务器的访问日志中服务器的访问日志是黑客攻击的重点对象之一 ...
- (转)iOS 开发,工程中混合使用 ARC 和非ARC
[前提知识] ARC:Automatic Reference Counting,自动引用计数 在开发 iOS 3 以及之前的版本的项目时我们要自己负责使用引用计数来管理内存,比如要手动 retain. ...
- iOS 开发,工程中混合使用 ARC 和非ARC(转)
[前提知识] ARC:Automatic Reference Counting,自动引用计数 在开发 iOS 3 以及之前的版本的项目时我们要自己负责使用引用计数来管理内存,比如要手动 retain. ...
- 【iOS开发】在ARC项目中使用非ARC文件
ARC的出现应该说是开发者的一大福利,苹果是推荐使用的,但是因为之前没有ARC机制,好多比较好的类库都是使用的非ARC,或是有些大牛还是不喜欢用ARC,封装的类也是非ARC的,想要在自己的ARC项目中 ...
- iOS开发——常见BUG——导航控制器中的子控制器设置StatusBar状态失效的问题
iOS9之前控制StatusBar的两种方式: 第一种方式:全局控制StatusBar 1. 在项目的Info.plist文件里设置UIViewControllerBasedStatusBarAppe ...
- iOS 开发总结(上)
来源:蝴蝶之梦天使 链接:http://www.jianshu.com/p/d333cf6ae4b0 在iOS开发中经常需要使用的或不常用的知识点的总结,几年的收藏和积累(踩过的坑). 一. iPho ...
- iOS开发-UITableView单选多选/复选实现1
TableView怎样实现单选或者多选呢? 我们的直接思路是改动某一个Cell的样式就可以, 那么改动样式须要通过改动相应的数据, 从这里能够判断我们须要给Cell相应的数据设置一个标志位, 当选中的 ...
随机推荐
- SpringBoot2 全局异常处理
参考这篇文章里面的几种异常形式: 全局异常处理是个比较重要的功能,一般在项目里都会用到. 大概把一次请求分成三个阶段,来分别进行全局的异常处理. 一:在进入Controller之前,譬如请求一个不存在 ...
- jq 获取当前屏幕高度
alert($(window).height()); //浏览器时下窗口可视区域高度 alert($(document).height()); //浏览器时下窗口文档的高度 alert($(docum ...
- Linux共享库 Linux内核链表
#ifndef __LINKEDLIST_H_ #define __LINKEDLIST_H_ #include <stdlib.h> #define offsetof(type, mem ...
- Mac 系统上安装Protocol buffer
1. cd /Software/protobuf-2.5.0 2.sudo ./configure --prefix=$/Software/protobuf-2.5.0 3.sudo make 4.s ...
- 【转】JS对Cookie的读写删除
JavaScript是运行在客户端的脚本,因此一般是不能够设置Session的,因为Session是运行在服务器端的.而cookie是运行在客户端的,所以可以用JS来设置cookie. 假设有这样一种 ...
- MySQL 服务(mysqld)crash
场景: 数据从 10.165.98.190 自建MySQL同步至 阿里云 MongoDB过程中,mysql服务会崩溃,同步数据失败. 原因分析: 经DBA 分析,10.165.98.1 ...
- gtest日志在工程项目中的应用
网上有各种gtest的入门教学,这里就不一一重复了.本文的目的是讲解如何将gtest应用于工程应用中.利用测试驱动开发这样的理论,来先写测试代码,当自动化测试跑通以后,主工程的代码也就编写完了. 这里 ...
- 正则表达式-----------------------------------C#的正则表达式
为了避免以后这样的情况,在此记录下正则表达式的一些基本使用方法附带小的实例.让以后在使用时能一目了然知道他的使用,为开发节约时间,同时也分享给大家 正则元字符 在说正则表达式之前我们先来看看通配符,我 ...
- Apache ZooKeeper
https://zookeeper.apache.org/ https://zh.wikipedia.org/wiki/Apache_ZooKeeper Apache ZooKeeper是Apache ...
- 本机IP、127.0.0.1和0.0.0.0的区别
本机ip.127.0.0.1和0.0.0.0区别 网络java IP地址的记法: IP地址由四个字节构成,为了方便阅读和书写,每个字节用0-255的数字表示,字节之间用’.'分割,如: 10.10 ...