tableView 单选

2016-06-03  本文已影响158人  jzhang
#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;
}
上一篇 下一篇

猜你喜欢

热点阅读