美文网首页
tableView 单选

tableView 单选

作者: jzhang | 来源:发表于2016-06-03 10:20 被阅读158次
#pragma mark - tableView delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    //self.indexPathSelect记录上一次选中的位置
    //把上一个选择的cell取消
    if (self.indexPathSelect) {
        UITableViewCell *selectCell = [tableView cellForRowAtIndexPath: self.indexPathSelect];
        selectCell.accessoryType = UITableViewCellAccessoryNone;
    }
    
    //更新记录,选中点击的cell
    self.indexPathSelect = indexPath;
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kIdentifier forIndexPath:indexPath];
    //根据记录的位置防止重用产生问题
    cell.accessoryType = (self.indexPathSelect == indexPath) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
    
    return cell;
}

相关文章

网友评论

      本文标题:tableView 单选

      本文链接:https://www.haomeiwen.com/subject/iggkdttx.html