iOS TableView实现单选checkmark功能
2014-08-28 15:56
901 查看
相信大家在做项目时有遇到需要实现这种功能---实现单选某一个cell表示选中
这个功能的实现只需要在两个方法中code即可
首选我们公开一个属性
@property(nonatomic,strong)NSIndexPath *lastPath;并且对其synthesize
主要是用来接收用户上一次所选的cell的indexpath
第一步:在cellForRowAtIndexPath:方法中实现如下代码
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSInteger row = [indexPath row];
NSInteger oldRow = [lastPath row];
if (row == oldRow && lastPath!=nil) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}else{
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
第二步:在didSelectRowAtIndexPath:中实现如下代码
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
int newRow = [indexPath row];
int oldRow = (lastPath !=nil)?[lastPath row]:-1;
if (newRow != oldRow) {
UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
newCell.accessoryType = UITableViewCellAccessoryCheckmark;
UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:lastPath];
oldCell.accessoryType = UITableViewCellAccessoryNone;
lastPath = indexPath;
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
Ok,可以收工了,这样实现之后的效果是每次单击一个cell会做一个选中的标志并且托动表视图时也不会出现checkmark的复用
希望对初学者有帮助到!
这个功能的实现只需要在两个方法中code即可
首选我们公开一个属性
@property(nonatomic,strong)NSIndexPath *lastPath;并且对其synthesize
主要是用来接收用户上一次所选的cell的indexpath
第一步:在cellForRowAtIndexPath:方法中实现如下代码
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSInteger row = [indexPath row];
NSInteger oldRow = [lastPath row];
if (row == oldRow && lastPath!=nil) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}else{
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
第二步:在didSelectRowAtIndexPath:中实现如下代码
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
int newRow = [indexPath row];
int oldRow = (lastPath !=nil)?[lastPath row]:-1;
if (newRow != oldRow) {
UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
newCell.accessoryType = UITableViewCellAccessoryCheckmark;
UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:lastPath];
oldCell.accessoryType = UITableViewCellAccessoryNone;
lastPath = indexPath;
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
Ok,可以收工了,这样实现之后的效果是每次单击一个cell会做一个选中的标志并且托动表视图时也不会出现checkmark的复用
希望对初学者有帮助到!
相关文章推荐
- ios 简单实现tableView单选和多选功能
- iOS 中使用tableView实现右滑显示选择功能
- iOS 开发-实现TableView的编辑功能以删除数据
- iOS中tableview实现单选和多选
- IOS UITableView实现单选功能
- ios TableView那些事(三十 五)TableView 单选操作使用Autolayout实现UITableView的Cell动态布局和高度动态改变
- iOS开发tableView实现单选和复选
- iOS tableview实现简单搜索功能
- iOS tableView实现单选和多选的实例代码
- iOS开发tableView实现单选和复选
- iOS开发之tableView实现左滑删除功能
- iOS中UITableView Cell实现自定义单选功能
- ios TableView那些事(三十 五)TableView 单选操作使用Autolayout实现UITableView的Cell动态布局和高度动态改变
- iOS开发之tableView cell的展开收回功能实现代码
- iOS 使用cell的删除功能,实现tableview的代理
- iOS中tableView上实现搜索功能
- iOS - UITableView 单选功能实现
- iOS tableView实现搜索功能
- iOS中tableview实现编辑、全选及删除等功能的方法示例
- iOS学习笔记-- tableView单选的实现