1

I have UITableView on iPad. When a cell is selected and the cell is scrolled off the screen, and then scrolled back into the screen, the selected cell is blank. My problem occurs only on the device and not on simulator. I have tried various of the proposed solutions like manage selected index

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.selectedIndexPath = indexPath;
}

and use setSelected/selectRowAtIndexPath on cellForRowAtIndexPath like

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Desktop Row";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell =[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    ...

    if(self.selectedIndexPath!=nil){
        if ((indexPath.row == self.selectedIndexPath.row) && (indexPath.section == self.selectedIndexPath.section)) {

            [self.tableView selectRowAtIndexPath:self.selectedIndexPath
                                        animated:YES
                                  scrollPosition:UITableViewScrollPositionNone];
        }
    }

    return cell;
}

But nothing works, I saw very similar questions people ask but I have something different and strage.

Any ideas will be appreciated.

2
  • the dequeueReusableCellWithIdentifier removes the cells that are scrolled out of the screen. So when it is scrolled back it is inserted as a new entry hence not selected.
    – Ahmed Z.
    May 21, 2013 at 7:13
  • 2
    I'm not sure, but you should move the selectRowAtIndexPath in an async block since when you call this method, the cell has not been created. alternatively, try -[UITableViewCell setSelected:]
    – maroux
    May 21, 2013 at 7:16

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.