Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What calls the setSelected:NO method automatically

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

-(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:

-(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:

- (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.

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:)

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

like image 203
Lukasz 'Severiaan' Grela Avatar asked Jun 05 '14 20:06

Lukasz 'Severiaan' Grela


1 Answers

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

like image 180
Danyun Liu Avatar answered Nov 08 '22 23:11

Danyun Liu