iOS开发-UITableView单选多选/复选实现1
TableView怎样实现单选或者多选呢?
我们的直接思路是改动某一个Cell的样式就可以,
那么改动样式须要通过改动相应的数据,
从这里能够判断我们须要给Cell相应的数据设置一个标志位,
当选中的时候来改动该标志位刷新那一行就可以
假设是单选实现略微复杂一些:
单选须要设置一个属性来保存上一次选中的行,
待选中新的行之后须要改动该行,不断维护
我的实现例如以下:
(1)创建一个TableViewController,
为了简单使用系统的Cell样式
设置重用标识符为 ACELL
cell相应的Model类为Person,
Person是Cell上相应的数据还包含是否选中的标志位
(2)导航栏的左边button用来提交单选的结果,右边button用来跳转到复选的界面
(3)关键代码
Person数据类,为cell提供数据
ifSelected属性的YES 或者 NO关乎是否该行cell被标记
//
// Person.h
// app39-表视图8-单选复选
//
// Created by MRBean on 15/7/24.
// Copyright (c) 2015年 yangbin. All rights reserved.
// #import <Foundation/Foundation.h>
@interface Person : NSObject
@property(copy,nonatomic)NSString *title;//cell上的textLabel数据
@property(copy,nonatomic)NSString *detail;//cell上的detailLabel数据
@property(assign,nonatomic)BOOL ifSelected;//是否选中
@end
TableViewController
//
// TableViewController.m
// app39-表视图8-单选复选
//
// Created by MRBean on 15/7/24.
// Copyright (c) 2015年 yangbin. All rights reserved.
// #import "TableViewController.h"
#import "Person.h"
@interface TableViewController ()
@property(strong,nonatomic)NSMutableArray *marr;//数据来源
@property(strong,nonatomic)NSIndexPath *lastSelected;//上一次选中的额索引
@end @implementation TableViewController
//初始时产生假数据
- (void)viewDidLoad {
[super viewDidLoad];
_marr = [[NSMutableArray alloc]init];
for (int i=0; i<20; i++)//产生大量假数据,使用系统的Cell
{
Person *p = [[Person alloc]init];
p.title = [NSString stringWithFormat:@"%iTitle",i];
p.detail = [NSString stringWithFormat:@"%iDetail",i];
p.ifSelected = NO;//是否被选中,默认都是NO
[_marr addObject:p];
}
}
#pragma mark - 数据源
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return _marr.count;
}
//配置每个cell的显示
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ACELL" forIndexPath:indexPath];
Person *p = _marr[indexPath.row];
cell.textLabel.text = p.title;//cell上的title显示
cell.detailTextLabel.text = p.detail;
//下面为关键代码1
if(p.ifSelected)//是否选中,假设为YES则标记
cell.accessoryType = UITableViewCellAccessoryCheckmark;//勾标记
else cell.accessoryType = UITableViewCellAccessoryNone;//不标记
return cell;
}
//选中一行cell后改变数据
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSIndexPath *temp = self.lastSelected;//暂存上一次选中的行
if(temp && temp!=indexPath)//假设上一次的选中的行存在,而且不是当前选中的这一样,则让上一行不选中
{
Person *tp = _marr[temp.row];
tp.ifSelected = NO;//改动之前选中的cell的数据为不选中
[tableView reloadRowsAtIndexPaths:@[temp] withRowAnimation:UITableViewRowAnimationAutomatic];//刷新该行
}
self.lastSelected = indexPath;//选中的改动为当前行
Person *p = _marr[indexPath.row];
p.ifSelected = YES;//改动这个被选中的一行choon
[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];//又一次刷新这一行
}
//点击提交,打印选中的结果
- (IBAction)tapSubmit:(UIBarButtonItem *)sender
{
Person *select = _marr[_lastSelected.row];
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"你选择的是:" message:select.title delegate:nil cancelButtonTitle:@"我知道了" otherButtonTitles:nil, nil];
[alert show];
}
//收到内存警告
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
执行效果:
单选效果
下一篇:TableView复选的实现http://blog.csdn.net/yangbingbinga/article/details/47057747
iOS开发-UITableView单选多选/复选实现1的更多相关文章
- 自动化测试-15.selenium单选框与复选框状态判断
本篇主要介绍单选框和复选框的操作 一.认识单选框和复选框 1.先认清楚单选框和复选框长什么样 2.各位小伙伴看清楚哦,上面的单选框是圆的:下图复选框是方的,这个是业界的标准,要是开发小伙伴把图标弄错了 ...
- 2.12 单选框和复选框(radiobox、checkbox)
2.12 单选框和复选框(radiobox.checkbox) 本篇主要介绍单选框和复选框的操作一.认识单选框和复选框 1.先认清楚单选框和复选框长什么样 2.各位小伙伴看清楚哦,上面的单选框是 ...
- Selenium2学习(十五)-- 单选框和复选框(radiobox、checkbox)
本篇主要介绍单选框和复选框的操作 一.认识单选框和复选框 1.先认清楚单选框和复选框长什么样 2.各位小伙伴看清楚哦,上面的单选框是圆的:下图复选框是方的,这个是业界的标准,要是开发小伙伴把图标弄错了 ...
- 微信小程序-修改单选框和复选框大小的方法
方法有两种: 一:采用css的zoom属性 zoom缩放会将元素保持在左上角,并且会有毛边,可能会稍稍改变元素原来的形状. 二:采用css3的transform:scale属性 zoom缩放会将元素保 ...
- Selenium3+python自动化 单选框和复选框
一.认识单选框和复选框 1.先认清楚单选框和复选框长什么样 2.各位小伙伴看清楚哦,上面的单选框是圆的:下图复选框是方的,这个是业界的标准,要是开发小伙伴把图标弄错了,可以先抽他了. 二.radio和 ...
- [原创]纯JS实现网页中多选复选框checkbox和单选radio的美化效果
图片素材: 最终效果图: <html><title> 纯JS实现网页中多选复选框checkbox和单选radio的美化效果</title><head>& ...
- CSS学习笔记三:自定义单选框,复选框,开关
一点一点学习CCS,这次学习了如何自定义单选框,复选框以及开关. 一.单选框 1.先写好body里面的样式,先写几个框 <body> <div class="radio-1 ...
- HTML--使用单选框、复选框,让用户选择
在使用表单设计调查表时,为了减少用户的操作,使用选择框是一个好主意,html中有两种选择框,即单选框和复选框,两者的区别是单选框中的选项用户只能选择一项,而复选框中用户可以任意选择多项,甚至全选.请看 ...
- jQuery获取单选框(复选框)选中的状态
jQuery 获取单选框(复选框)选中的状态 <input type="checkbox" name="" id="choose"/& ...
随机推荐
- \p{space}|\p{Hex}|\p{Digit}|转译符|\1|\g{-1}|[^ab]|/([^\d\D]+)/
小骆驼 第七章 漫游正则表达式王国 #!/usr/bin/perl use strict; use warnings; $_ = 'ab \ cde f ghijk10.x12ln'; if(/d/) ...
- 什么是session?
Session一般译作会话.从不同的层面看待session,它有着类似但不全然相同的含义.比如,在web应用的用户看来,他打开浏览器访问一个电子商务网站,登录.并完成购物直到关闭浏览器,这是一个会话. ...
- 用Multisim实现彩灯循环控制器
2019/06/06 !转载请注明出处 1.设计任务目的与要求 1.1 展示器件 10路彩灯分别用10个发光二极管L0.L1…..L9模拟,发光二极管L0.L1…..L9从左到右排列. 1.2 要求显 ...
- POJ-3190-分配畜栏
这个题首先,我们需要注意的是它的时间是一秒,其中还包括了你读入数据的时间,因为cin我写的时候没有解除绑定,所以直接超时,我们直接用scanf函数读入50000组数据好了. 然后就是poj交的时候,如 ...
- POJ-3624-背包问题
它这个问题问的是,在有限的容量下,能装下的最大价值是多少. 所以我们可以递归求解,记忆性递归,用二维数组,但是这样的话就会超内存,所以我们只能用动规来写,而且不能开二维数组, 只能用滚动数组. 我们设 ...
- kvm使用kickstart文件自动安装系统
假定kvm已经准备好 1.创建磁盘 qemu-img create -f qcow2 /kvm/os/vm-01.qcow2 16G 2.上传或下载安装镜像 mkdir -p /kvm/iso cd ...
- poj 3683 2-sat问题,输出任意一组可行解
/* 2sat问题 输出任意一组可行解 */ #include<stdio.h> #include<string.h> #include<stdlib.h> #in ...
- Flask处理前端POST过来的JSON数据
POST JSON数据的JS代码: $.ajax({ url:'http://127.0.0.1:5000/calc', type : 'post', dataType:'json', headers ...
- @JoinColumn 匹配关联多个字段
两张表结构如下 TABLE_A: ID, COLA1, COLA2 TABLE_B: ID, A_ID, COLB1, COLB2 A和B是一对多的关系. 我在B的BEAN上面,通过Anotation ...
- Java图片缩略图裁剪水印缩放旋转压缩转格式-Thumbnailator图像处理
前言 java开发中经常遇到对图片的处理,JDK中也提供了对应的工具类,不过处理起来很麻烦,Thumbnailator是一个优秀的图片处理的开源Java类库,处理效果远比Java API的好,从API ...