您的位置:首页 > 移动开发 > IOS开发

iOS学习笔记-- tableView单选的实现

2017-04-27 12:58 579 查看
在iOS开发中,常常会碰到在列表中实现单选的功能,在tableView中,可以通过其自身来实现。

在这里定义tagIndex来标记当前选中的行
@property (assign, nonatomic) NSIndexPath *tagIndex;


在-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath这个方法中通过tagIndex来判断当前行是否被选中。


if(_tagIndex == indexPath)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}


在-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath这个方法中


//如果是之前选中的,则取消选择
UITableViewCell *celled = [tableView cellForRowAtIndexPath:_tagIndex];
celled.accessoryType = UITableViewCellAccessoryNone;
//记录当前选中的位置
_tagIndex = indexPath;
//当前选择的打勾
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;


如果想要改变对号的颜色,可用cell.TintColor来设置。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios uitableviewcell