如何处理Tableview中cell的单选问题
2015-12-10 12:58
295 查看
首先声明几个属性
在- (void)viewDidLoad中进行适当初始化
第一种方法
第二种方法:
@property (nonatomic, strong) NSMutableArray * dataArr; //数据源 @property (nonatomic, copy) NSString * current; //数据源中对象 @property (nonatomic, assign) NSInteger currentIndex; //当前点击的cell的索引
在- (void)viewDidLoad中进行适当初始化
{ self.current = @""; self.currentIndex = -1; }
第一种方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString * identify = @"cell"; UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identify]; if (!cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identify]; } //在cell复用的时候判断是否该cell是被选中的,需要保持选中状态. if ([self.current isEqualToString:self.dataArr[indexPath.row]]) { cell.accessoryType = UITableViewCellAccessoryCheckmark; } else { cell.accessoryType = UITableViewCellAccessoryNone; } cell.textLabel.text = [NSString stringWithFormat:@"%@", self.dataArr[indexPath.row]]; return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:YES]; //取出之前选择的cell在数据源中的索引,如果是第一次选择,则self.current为nil. NSInteger index = [self.dataArr indexOfObject:self.current]; if (index == indexPath.row) { return; } //记录之前选择的索引 NSIndexPath * old = [NSIndexPath indexPathForRow:index inSection:0]; //得到当前cell UITableViewCell * newCell = [tableView cellForRowAtIndexPath:indexPath]; if (newCell.accessoryType == UITableViewCellAccessoryNone) { newCell.accessoryType = UITableViewCellAccessoryCheckmark; self.current = [self.dataArr objectAtIndex:indexPath.row]; } //得到上次选择的cell UITableViewCell * oldCell = [tableView cellForRowAtIndexPath:old]; if (oldCell.accessoryType == UITableViewCellAccessoryCheckmark) { oldCell.accessoryType = UITableViewCellAccessoryNone; } }
第二种方法:
用记录选择的cell的索引self.currentIndex,初始化时设置为-1. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString * identify = @"cell"; UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identify]; if (!cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identify]; } //在cell复用的时候判断是否该cell是被选中的,需要保持选中状态. if (self.currentIndex == indexPath.row) { cell.accessoryType = UITableViewCellAccessoryCheckmark; } else { cell.accessoryType = UITableViewCellAccessoryNone; } cell.textLabel.text = [NSString stringWithFormat:@"%@", self.dataArr[indexPath.row]]; return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:YES]; //取出之前选择的cell在数据源中的索引,如果是第一次选择,则self.current为nil. if (self.currentIndex == indexPath.row) { return; } //记录之前选择的索引 NSIndexPath * old = [NSIndexPath indexPathForRow:self.currentIndex inSection:0]; //得到当前cell UITableViewCell * newCell = [tableView cellForRowAtIndexPath:indexPath]; if (newCell.accessoryType == UITableViewCellAccessoryNone) { newCell.accessoryType = UITableViewCellAccessoryCheckmark; self.currentIndex = indexPath.row; } //得到上次选择的cell UITableViewCell * oldCell = [tableView cellForRowAtIndexPath:old]; if (oldCell.accessoryType == UITableViewCellAccessoryCheckmark) { oldCell.accessoryType = UITableViewCellAccessoryNone; }
相关文章推荐
- 从长板和咏春看单板滑雪固定器角度选择
- Linux(Centos6.5)下安装svn服务器,并通过http访问
- 解决MySQL不允许从远程访问
- 设计模式之创建模式
- OLTP和OLAP
- LeetCode - 12. Integer to Roman
- 奇异的恩典
- Html知识总结
- svn提交时强制注释
- 扩展欧拉函数
- 机器学习-KNN 算法
- 3K工资与8K工资的差距,不仅仅是钱!
- LeetCode - 11. Container With Most Water
- Centos配置80 端口转发
- jQuery-Ajax的一点小经验
- Install Shiny Server in Ubuntu 14.04.1
- 基于第三方WheelView 实现的一个时间选择器
- 笔记本外接显示器鼠标从左边进入
- Install R & RStudio in Ubuntu
- 博客编写客户端分享