什么调用setSelected:NO方法自动

时间:2014-06-05 20:13:13

标签: ios uitableview

我对UITableView和UITableViewCell有这个奇怪的问题。只要调用-tableView:cellForRowAtIndexPath:,就会调用setSelected:NO。

-(UITableViewCell *)tableView:(UITableView *)tableView
        cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    NSDictionary *cellData = dataSource[indexPath.row];

    NSString *cellId = nil;
    //choose cell type
    NSInteger type = [[cellData valueForKey:@"type"] integerValue];
    switch (type) {
        case 0:
            //root level
            cellId = @"rootHeaderCell";
            break;
        case 1:
            //sub header
            cellId = @"subHeaderCell";
            break;
        case 2:
        default:
            //value row - radiobutton
            cellId = @"radiobuttonCell";
            break;
    }

    CSBaseFilterCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId
                                                            forIndexPath:indexPath];

    // Configure the cell...
    [cell setData:cellData];

    return cell;
}

cellData包含有关选择和单元格的信息:

-(void)setData:(NSDictionary *)data {

NSLog(@"Set Data");
NSLog(@"data has selected=%d", [data hasKey:@"selected"]);
NSLog(@"data.selected=%@", [data valueForKey:@"selected"]);

    if ([data hasKey:@"selected"]) {
        [self setSelected:[[data valueForKey:@"selected"] boolValue]
                 animated:NO];

    }
}

我设置了选择,但没有选中。我在setData中添加了跟踪,并且值很好,我在setSelected:animated:中添加了跟踪,发现有3个调用:

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
    NSLog(@"%@ setSelected:selected=%d",self,selected);


    self.accessoryType = selected ? UITableViewCellAccessoryCheckmark:UITableViewCellAccessoryNone;
}

这是日志输出中显示的内容:

2014-06-05 21:11:59.679 UITableView Filter Sort[42821:60b] Set Data
2014-06-05 21:11:59.679 UITableView Filter Sort[42821:60b] data has selected=1
2014-06-05 21:11:59.680 UITableView Filter Sort[42821:60b] data.selected=1
2014-06-05 21:11:59.680 UITableView Filter Sort[42821:60b] <CSRadioButtonCell: 0x8c80630; baseClass = UITableViewCell; frame = (0 64; 320 32); autoresize = W; layer = <CALayer: 0x8c76920>> setSelected:selected=1
2014-06-05 21:11:59.686 UITableView Filter Sort[42821:60b] <CSRadioButtonCell: 0x8c80630; baseClass = UITableViewCell; frame = (0 64; 320 32); autoresize = W; layer = <CALayer: 0x8c76920>> setSelected:selected=0
2014-06-05 21:11:59.811 UITableView Filter Sort[42821:60b] <CSRadioButtonCell: 0x8c80630; baseClass = UITableViewCell; frame = (0 64; 320 32); autoresize = W; layer = <CALayer: 0x8c76920>> setSelected:selected=0

表放在ViewController中,它不是UITableViewController。

更新 我已将setData:call放入willDisplayCell:forRowAtIndexPath:并删除了对setSelected的超级定义的调用,这解决了这个问题。它仍然在日志中显示,在cellForRowAtIndexPathwillDisplayCell:forRowAtIndexPath:中的setData之间使用NO参数调用setSelected,但至少willDisplay是最后一个:)

我也尝试使用checkedsetData:中设置的我自己的“选择”属性didSelectRowAtIndexPath:

1 个答案:

答案 0 :(得分:6)

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

中执行setSelected