TableView怎样实现单选或者多选呢?

我们的直接思路是改动某一个Cell的样式就可以,

那么改动样式须要通过改动相应的数据,

从这里能够判断我们须要给Cell相应的数据设置一个标志位,

当选中的时候来改动该标志位刷新那一行就可以

假设是单选实现略微复杂一些:

单选须要设置一个属性来保存上一次选中的行,

待选中新的行之后须要改动该行,不断维护

我的实现例如以下:

(1)创建一个TableViewController,

为了简单使用系统的Cell样式

设置重用标识符为 ACELL

cell相应的Model类为Person,

Person是Cell上相应的数据还包含是否选中的标志位

(2)导航栏的左边button用来提交单选的结果,右边button用来跳转到复选的界面

(3)关键代码

Person数据类,为cell提供数据

ifSelected属性的YES 或者 NO关乎是否该行cell被标记

  1. //
  2. // Person.h
  3. // app39-表视图8-单选复选
  4. //
  5. // Created by MRBean on 15/7/24.
  6. // Copyright (c) 2015年 yangbin. All rights reserved.
  7. //
  8.  
  9. #import <Foundation/Foundation.h>
  10. @interface Person : NSObject
  11. @property(copy,nonatomic)NSString *title;//cell上的textLabel数据
  12. @property(copy,nonatomic)NSString *detail;//cell上的detailLabel数据
  13. @property(assign,nonatomic)BOOL ifSelected;//是否选中
  14. @end

TableViewController

  1. //
  2. // TableViewController.m
  3. // app39-表视图8-单选复选
  4. //
  5. // Created by MRBean on 15/7/24.
  6. // Copyright (c) 2015年 yangbin. All rights reserved.
  7. //
  8.  
  9. #import "TableViewController.h"
  10. #import "Person.h"
  11. @interface TableViewController ()
  12. @property(strong,nonatomic)NSMutableArray *marr;//数据来源
  13. @property(strong,nonatomic)NSIndexPath *lastSelected;//上一次选中的额索引
  14. @end
  15.  
  16. @implementation TableViewController

//初始时产生假数据

  1. - (void)viewDidLoad {
  2. [super viewDidLoad];
  3. _marr = [[NSMutableArray alloc]init];
  4. for (int i=0; i<20; i++)//产生大量假数据,使用系统的Cell
  5. {
  6. Person *p = [[Person alloc]init];
  7. p.title = [NSString stringWithFormat:@"%iTitle",i];
  8. p.detail = [NSString stringWithFormat:@"%iDetail",i];
  9. p.ifSelected = NO;//是否被选中,默认都是NO
  10. [_marr addObject:p];
  11. }
  12. }

#pragma mark - 数据源

  1. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  2. return 1;
  3. }
  1. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  2. return _marr.count;
  3. }

//配置每个cell的显示

  1. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  2. {
  3. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ACELL" forIndexPath:indexPath];
  4. Person *p = _marr[indexPath.row];
  5. cell.textLabel.text = p.title;//cell上的title显示
  6. cell.detailTextLabel.text = p.detail;
  7. //下面为关键代码1
  8. if(p.ifSelected)//是否选中,假设为YES则标记
  9. cell.accessoryType = UITableViewCellAccessoryCheckmark;//勾标记
  10. else cell.accessoryType = UITableViewCellAccessoryNone;//不标记
  11. return cell;
  12. }

//选中一行cell后改变数据

  1. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  2. {
  3. NSIndexPath *temp = self.lastSelected;//暂存上一次选中的行
  4. if(temp && temp!=indexPath)//假设上一次的选中的行存在,而且不是当前选中的这一样,则让上一行不选中
  5. {
  6. Person *tp = _marr[temp.row];
  7. tp.ifSelected = NO;//改动之前选中的cell的数据为不选中
  8. [tableView reloadRowsAtIndexPaths:@[temp] withRowAnimation:UITableViewRowAnimationAutomatic];//刷新该行
  9. }
  10. self.lastSelected = indexPath;//选中的改动为当前行
  11. Person *p = _marr[indexPath.row];
  12. p.ifSelected = YES;//改动这个被选中的一行choon
  13. [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];//又一次刷新这一行
  14. }

//点击提交,打印选中的结果

  1. - (IBAction)tapSubmit:(UIBarButtonItem *)sender
  2. {
  3. Person *select = _marr[_lastSelected.row];
  4. UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"你选择的是:" message:select.title delegate:nil cancelButtonTitle:@"我知道了" otherButtonTitles:nil, nil];
  5. [alert show];
  6. }

//收到内存警告

  1. - (void)didReceiveMemoryWarning {
  2. [super didReceiveMemoryWarning];
  3. // Dispose of any resources that can be recreated.
  4. }

@end

执行效果:

单选效果

下一篇:TableView复选的实现http://blog.csdn.net/yangbingbinga/article/details/47057747

相关文章:http://blog.csdn.net/yangbingbinga

iOS开发-UITableView单选多选/复选实现1的更多相关文章

  1. 自动化测试-15.selenium单选框与复选框状态判断

    本篇主要介绍单选框和复选框的操作 一.认识单选框和复选框 1.先认清楚单选框和复选框长什么样 2.各位小伙伴看清楚哦,上面的单选框是圆的:下图复选框是方的,这个是业界的标准,要是开发小伙伴把图标弄错了 ...

  2. 2.12 单选框和复选框(radiobox、checkbox)

    2.12 单选框和复选框(radiobox.checkbox) 本篇主要介绍单选框和复选框的操作一.认识单选框和复选框    1.先认清楚单选框和复选框长什么样 2.各位小伙伴看清楚哦,上面的单选框是 ...

  3. Selenium2学习(十五)-- 单选框和复选框(radiobox、checkbox)

    本篇主要介绍单选框和复选框的操作 一.认识单选框和复选框 1.先认清楚单选框和复选框长什么样 2.各位小伙伴看清楚哦,上面的单选框是圆的:下图复选框是方的,这个是业界的标准,要是开发小伙伴把图标弄错了 ...

  4. 微信小程序-修改单选框和复选框大小的方法

    方法有两种: 一:采用css的zoom属性 zoom缩放会将元素保持在左上角,并且会有毛边,可能会稍稍改变元素原来的形状. 二:采用css3的transform:scale属性 zoom缩放会将元素保 ...

  5. Selenium3+python自动化 单选框和复选框

    一.认识单选框和复选框 1.先认清楚单选框和复选框长什么样 2.各位小伙伴看清楚哦,上面的单选框是圆的:下图复选框是方的,这个是业界的标准,要是开发小伙伴把图标弄错了,可以先抽他了. 二.radio和 ...

  6. [原创]纯JS实现网页中多选复选框checkbox和单选radio的美化效果

    图片素材: 最终效果图: <html><title> 纯JS实现网页中多选复选框checkbox和单选radio的美化效果</title><head>& ...

  7. CSS学习笔记三:自定义单选框,复选框,开关

    一点一点学习CCS,这次学习了如何自定义单选框,复选框以及开关. 一.单选框 1.先写好body里面的样式,先写几个框 <body> <div class="radio-1 ...

  8. HTML--使用单选框、复选框,让用户选择

    在使用表单设计调查表时,为了减少用户的操作,使用选择框是一个好主意,html中有两种选择框,即单选框和复选框,两者的区别是单选框中的选项用户只能选择一项,而复选框中用户可以任意选择多项,甚至全选.请看 ...

  9. jQuery获取单选框(复选框)选中的状态

    jQuery 获取单选框(复选框)选中的状态 <input type="checkbox" name="" id="choose"/& ...

随机推荐

  1. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown error 1146

    问题介绍:   com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown error 1146 MySql语法错误, 但是错 ...

  2. Python List extend()方法

    Python List extend()方法  Python 列表 描述 extend() 函数用于在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表). 语法 extend()方法语法 ...

  3. xcode菜单栏

    File  文件 Edit  编辑 View 视图 Navigate 导航 Editor 编辑 Product 产品 Window  窗口 Help 帮助 File  文件 New 新建        ...

  4. uncategorized SQLException for SQL []; SQL state [99999]; error code [17004]; 无效的列类型: 1111; nested exception is java.sql.SQLException: 无效的列类型: 1111

    WHERE 的条件取值时添加上jdbcType=NUMBER这样的配置 参考[1]:https://blog.csdn.net/fyhjuyol/article/details/45483167

  5. 【struts2】学习笔记

    常见问题及注意事项: 1.下载struts2时,要看清所下载的版本,不同版本web.xml配置路径不同! 2. 导入jar包时,导入的包要完全准确,缺少或过多的会导致缺失或冲突! 3. Registe ...

  6. 使用window.open打开新窗口被浏览器拦截的解决方案

    问题描述: 代码中直接使用window.open('//www.baidu.com', '_blank');会被浏览器窗口拦截 原因浏览器为了维护用户安全和体验,在JS中直接使用window.open ...

  7. 初识Pyhon

    如果你的系统安装了这两个版本,请使用Python 3 如果没有安装Python,请安装Python 3 主要介绍在windows 64位操作系统上安装Python 3 1. 安装Python 首先,检 ...

  8. 【BZOJ 1588】[HNOI2002] 营业额统计(Treap)

    Description 营业额统计 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger拿出了公司的账本,账本上记录了公司成立以来每 ...

  9. POJ 2728 Desert King(最优比率生成树, 01分数规划)

    题意: 给定n个村子的坐标(x,y)和高度z, 求出修n-1条路连通所有村子, 并且让 修路花费/修路长度 最少的值 两个村子修一条路, 修路花费 = abs(高度差), 修路长度 = 欧氏距离 分析 ...

  10. Sticky footer经典布局--绝对底部布局

    原文转载于:https://cnodejs.org/topic/56ebdf2db705742136388f71 何为Sticky footer布局? 我们常见的网页布局方式一般分为header(页头 ...