什么调用setSelected:NO方法自动

时间:2022-09-25 18:30:53

I have this weird problem with UITableView and UITableViewCell. Whenever the -tableView:cellForRowAtIndexPath: is called the setSelected:NO is called afterwards.

我有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 contains information about selection and in 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];

    }
}

I set the selection, but it was not selected. I've put traces in setData and the values were fine, I have put trace in setSelected:animated: and found that 3 calls are made:

我设置了选择,但未选中。我在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;
}

this is what is displayed on log output:

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

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

Table is placed in the ViewController, it is not UITableViewController.

表放在ViewController中,它不是UITableViewController。

UPDATE I have put the setData: call in willDisplayCell:forRowAtIndexPath: and removed call to super definition of setSelected and this resolved the issue. It still shows in logs that setSelected is called with NO argument between setData in cellForRowAtIndexPath and willDisplayCell:forRowAtIndexPath: but at least willDisplay is last:)

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

I have also tried successfully with my own "selection" property checked which is set in setData: and in didSelectRowAtIndexPath:.

我也尝试使用我自己的“selection”属性检查,该属性在setData:和didSelectRowAtIndexPath:中设置。

1 个解决方案

#1


6  

Do the setSelected in - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

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

#1


6  

Do the setSelected in - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

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