UITableViewCell 单选

来源:互联网 发布:js验证手机号码 编辑:程序博客网 时间:2024/06/07 01:19
1.定义属性
 @property (nonatomic, assign)     int rowIndex;
2.
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];// 当前cell
    
    if (_rowIndex == indexPath.row)
    {
        if (cell.accessoryType == UITableViewCellAccessoryNone)
        {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        }
        else
        {
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
    }
    else
    {
        // 取消前一个选中的,就是单选啦
        NSIndexPath *lastIndex = [NSIndexPath indexPathForRow:_rowIndex inSection:0];
        UITableViewCell *lastCell = [tableView cellForRowAtIndexPath:lastIndex];
        lastCell.accessoryType = UITableViewCellAccessoryNone;
        // 修改选中的cell未选择
        cell.accessoryType    = UITableViewCellAccessoryCheckmark;
    }
    
    // 保存选中的
    _rowIndex = indexPath.row;
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
0 0
原创粉丝点击