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

来源:互联网 发布:最小的windows 编辑:程序博客网 时间:2024/05/21 09:57

在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来设置。

1 0