Programmatically highlight UITableView cell

I have an iPad app which uses a UISplitViewController (with a UITableView on the left and a detail view on the right). My table view highlights the selected cell in blue when you tap on it.

When I call the following method, the cell is selected but not highlighted in blue:

[self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionTop];

I have spent literally days fiddling about with various delegate methods and hacks trying to get the cell to highlight programatically just as if it had been tapped. I can't do it.

I've managed to almost get there with this:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (shouldHighlightCell)
    {
        NSIndexPath *indexPathForCellToHighlight = [NSIndexPath indexPathForRow:0 inSection:0];

        if ([indexPath isEqual:indexPathForCellToHighlight])
        {
            cell.selected = YES;
            shouldHighlightCell = NO;
        }
    }
}

It works as long as I also have this (otherwise it remains selected even when another cell is tapped):

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSIndexPath *ip = [NSIndexPath indexPathForRow:0 inSection:0];

    if ([[self.tableView cellForRowAtIndexPath:ip] isSelected])
    {
        [[self.tableView cellForRowAtIndexPath:ip] setSelected:NO];
    }

    NSIndexPath *iToTheP = indexPath;
    return iToTheP;
}

I know this is a weird and convoluted workaround. I wouldn't mind, but it doesn't even work fully. The selected cell loses its highlight if it is scrolled off screen, whereas a cell that has been tapped remains highlighted when scrolled off screen.

I'm absolutely baffled by this. I'm sure this workaround shouldn't even be necessary, that there is a much simpler solution.

Please be sure the cell'sselectionStyleisUITableViewCellSelectionStyleBlueand the tableView'sallowsSelectionis set toYES.

The methodselectRowAtIndexPath:animated:scrollPosition:works fine for me. It does highlight the selected cell.

I also tried many approaches to get the initial selection to display on my single-selection UITableView. What finally worked for me was to defer the selection of the initial row until the table was set up by calling it in my UITableViewController's viewDidAppear:

override func viewDidAppear(animated: Bool)
{
    tableView.selectRowAtIndexPath(indexPathToSelectInitially, animated: false, scrollPosition: .None)
}

I found this and it works for me (aka calling the delegate method didSelectRowAtIndexPath)

NSIndexPath *defaultIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self tableView:[self tableView] didSelectRowAtIndexPath:defaultIndexPath];

PS. I'm using UITableViewController.

I found this to be completely unfixable using all known possibilities. In the end I fixed it by ditching a lot of my code and switching to NSFetchedResultsController instead. NSFetchedResultsController was introduced shortly after I originally wrote this app, and it greatly simplifies the process of using Core Data with UITableViews.https://developer.apple.com/library/IOs/documentation/CoreData/Reference/NSFetchedResultsController_Class/index.html

I went through and tried all these and other solutions and no joy. In my case the problem (which drove me nuts for 2 hrs) was the following - shortly after I was callingselectRowAtIndexPath, I was callingreloadDataon thetableview. That reload was wiping all the highlighting! Beware of this pitfall! With the unnecessary reloading of data call gone, the highlighting happenned as expected.

It gets the backgroundview with cell border looking like seperator.Do not change the default tableview settings in Interface builder.Make sure UITableViewCellSelectionStyleNone is NOT set to selectionstyle. I am pasting the working code. :

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *kCellIdentifier = @"PlayListCell";
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:kCellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:kCellIdentifier];
    }
    MPMediaPlaylist *playList = [playlistCollection objectAtIndex:indexPath.row];
    cell.textLabel.text = playList.name;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
     // cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%d Songs",[playList.items count]];
    MPMediaItemCollection *playListMediaCollection = [playlistCollection objectAtIndex:indexPath.row ];

    cell.imageView.image =[UIImage imageWithCGImage:[self getImageForCollection:playListMediaCollection.items]];

    // the main code which make it highlight

    UIView *bgColorView = [[UIView alloc] init];
    bgColorView.backgroundColor = [UIColor colorWithRed:170.0f/255.0 green:170.0f/255.0 blue:170.0f/255.0 alpha:1.0f];
    [bgColorView.layer setBorderColor:[UIColor blackColor].CGColor];
    [bgColorView.layer setBorderWidth:1.0f];
    [cell setSelectedBackgroundView:bgColorView];
    return cell;
}
What Others Are Reading