Stuck at tableView:didSelectRowAtIndexPath:


#1

As far as I know everything was working fine up until when I added tableView:didSelectRowAtIndexPath: in ItemsViewController.m (p. 230 for those with the book). When I build and run and add an item the new item appears. But when I tap on it it highlights, and remains highlighted, but that’s it. I added a breakpoint to tableView:didSelectRowAtIndexPath: and it is not called.

So I added tableView:willSelectRowAtIndexPath:. It is called and it has the correct row index of 0. But tableView:didSelectRowAtIndexPath: is still not called.

So I add another item. It appear as it should but, oddly, the original item above it remains highlighted. If I click the original item I still get the same behaviour. tableView:willSelectRowAtIndexPath: is called (with the correct row and section indexes) but not tableView:didSelectRowAtIndexPath:.

If I click on the new item tableView:willSelectRowAtIndexPath: gets called (with the correct row and section indexes, 1 and 0 respectively) and then tableView:didSelectRowAtIndexPath: is called but the row index is now 0 instead of 1.

As part of the debugging process I added breakpoints to these methods but everything seems to be fine. The counts and indexes seem to be correct.

  • tableView:numberOfRowsInSection:
  • tableView:cellForRowAtIndexPath:
  • addNewItem:
  • tableView:willSelectRowAtIndexPath:
  • tableView:didSelectRowAtIndexPath:

Except that tableView:didSelectRowAtIndexPath: is getting called with an index one less than what was actually selected.

I can include code but I was hoping that someone might have the quick “You forgot this…” answer.

Murray =:-)


#2

Found it. Embarrassingly simple. I was mistakenly calling

tableView:didDeselectRowAtIndexPath: instead of
tableView:didSelectRowAtIndexPath:

Solved.


#3

Thanks for posting this. i had the same problem.


#4

Thanks, also, for posting this. I was bitten again by not distinguishing two similarly named methods in the code completion dialog: didDeselectRowAtIndexPath: and didSelectRowAtIndexPath: You can drive yourself crazy by never suspecting you’ve coded the wrong method, and instead placing breakpoints all over the place trying to figure out why the “latest framework” doesn’t work with the authors code.