title: Programmatically highlight UITableView cell
body: <p>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.</p>
  
  <p>When I call the following method, the cell is selected but not highlighted in blue:</p>
  
  <pre><code>[self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionTop];
  </code></pre>
  
  <p>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.</p>
  
  <p>I've managed to almost get there with this:</p>
  
  <pre><code>- (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;
          }
      }
  }
  </code></pre>
  
  <p>It works as long as I also have this (otherwise it remains selected even when another cell is tapped):</p>
  
  <pre><code>- (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;
  }
  </code></pre>
  
  <p>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.</p>
  
  <p>I'm absolutely baffled by this. I'm sure this workaround shouldn't even be necessary, that there is a much simpler solution. </p>