iOS开发-UITableView单选多选/复选实现1
来源:互联网 发布:水波算法 编辑:程序博客网 时间:2024/05/22 00:09
TableView如何实现单选或者多选呢?
我们的直接思路是修改某一个Cell的样式即可,
那么修改样式需要通过修改对应的数据,
从这里可以推断我们需要给Cell对应的数据设置一个标志位,
当选中的时候来修改该标志位刷新那一行即可
如果是单选实现稍微复杂一些:
单选需要设置一个属性来保存上一次选中的行,
待选中新的行之后需要修改该行,不断维护
我的实现如下:
(1)创建一个TableViewController,
为了简单使用系统的Cell样式
设置重用标识符为 ACELL
cell对应的Model类为Person,
Person是Cell上对应的数据还包括是否选中的标志位
(2)导航栏的左边按钮用来提交单选的结果,右边按钮用来跳转到复选的界面
(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;}
- (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
相关文章:http://blog.csdn.net/yangbingbinga
1 0
- iOS开发-UITableView单选多选/复选实现1
- iOS-开发UITableView-1
- IOS开发中实现复选框
- iOS开发tableView实现单选和复选
- iOS开发tableView实现单选和复选
- Android 开发 实现复选
- IOS开发笔记30-UITableView(1)
- IOS开发笔记33-UITableView(1)
- iOS开发>>>UITableView
- Xamarin ios开发 UITableView
- 【IOS开发】UITableView详解
- iOS开发-UITableView-2
- iOS开发 ----- UITableView
- iOS开发UITableView小结
- iOS开发基础 - UITableView
- 系列:iOS开发-UITableView
- iOS 底层实现 - UITableView
- iOS开发之UITableView的下拉刷新实现
- jQuery中用于筛选的方法总结
- Tomcat Server配置
- UItableview x详解
- c++模板选择排序
- python模拟ctrl+v
- iOS开发-UITableView单选多选/复选实现1
- python实现类似switch功能
- Shared libraries with Eclipse CDT and cygwin on Windows
- struts 验证规则
- Snail—OC学习之空变量的表示
- Codeforces Round #282 (Div. 2) C. Treasure 贪心
- JS中Null与Undefined的区别
- mysql 输出表的详细信息详解 show table status like '表名'
- Code Forces 535 B. Tavas and SaDDas(数论)