1

I am trying to programatically highlight a table view cell and trigger the selection logic by doing the following

NSIndexPath*indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
[self tableView:self.tableView didSelectRowAtIndexPath:indexPath];

The row highlights only for a split second. I want it to stay highlighted until I select another row.

I tried adding these lines

UITableViewCell *cell = [self.tableView cellForRowAtIndexPath: indexPath];
cell.highlighted = YES;

but when I did this, the highlight remained even when I clicked on another row and did not go away until I clicked the first row again.

Any ideas?

Heisenberg
  • 1,319
  • 1
  • 12
  • 15
  • One thing I should mention is that the table view controller is contained in a container view controller, seems like that might be affecting the highlighting, not sure why – Heisenberg Jul 09 '13 at 22:22
  • I moved the calling code to viewDidAppear, seems like there was an issue with it just being in viewDidLoad, this seems to fix the issue – Heisenberg Jul 09 '13 at 22:30
  • Yeah you shouldn't be selecting cells in viewDidLoad... – rocky Jul 09 '13 at 22:44

2 Answers2

1

Try calling selectRowAtIndexPath but not didSelectRowAtIndexPath. I believe the latter is called as a result of the former. If your delegate deSelects the last selected index path in didSelectRowAtIndexPath, then the double call would result in deselecting what you had just selected

KHansenSF
  • 604
  • 4
  • 7
  • Thanks but that did not fix it, I added another comment about the table view controller being contained in another view controller, I wonder if that is what is causing the issue – Heisenberg Jul 09 '13 at 22:23
  • Can you post the relevant code? Are you using a storyboard or nib at all? – KHansenSF Jul 09 '13 at 22:42
  • seems to be working now, I think the key thing is that the tableviewcontroller was displayed in another container view controller, initialy I was only calling the above code from the viewDidLoad in the container, something must have happened after that to clear the selection. I changed it to call the code from the viewDidAppear and now its ok – Heisenberg Jul 09 '13 at 22:48
0

seems like the issue was because I was calling the code from viewDidLoad, I moved it to viewDidAppear and now its fine

Heisenberg
  • 1,319
  • 1
  • 12
  • 15