DidSelectRowAtIndexPath returns an invalid IndexPath

I ran into a really cryptic error. The first line of my UITableView returns 1 , and second returns 0 in indexPath! How is this possible?

In my `- (void) viewDidLoad` everything is still fine. I highlight the first line successfully with

currentRow = 0; [tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:currentRow inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone]; 

I have a currentRow variable to track the selected row (another control changes depending on the currently selected one).

Now in my delegate function `didDeselectRowAtIndexPath` I have:

 -(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath { ... NSLog(@"IndexPath: %@", [indexPath description]); } 

The following is displayed in the log:

IndexPath: <NSIndexPath 0x79509d0> 2 indexes [0, 0] when I touch the second line and IndexPath: <NSIndexPath 0x79509d0> 2 indexes [0, 1] when I touch the first line.

No insertion or deletion of rows or sorting, etc., not even scrolling. This is a simple UITableView, grouped style, with 1 section and 3 rows. What could be the reason for this?

Thanks for your help,
S

+68
objective-c iphone uitableview didselectrowatindexpath nsindexpath
Nov 07 '10 at 18:14
source share
2 answers

You deployed made De selectRowAtIndexPath . It will be launched when the row is no longer selected.

When you touch the second line, the first line will no longer be selected, so [0, 1] will be displayed.
When you touch the first line again, the second line will no longer be selected, so [0, 0] will be displayed.

This is fully expected.

Implement did Select RowAtIndexPath if you need it to respond when a row is selected.

 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { // ^ ... NSLog(@"IndexPath: %@", [indexPath description]); } 
+223
Nov 07 '10 at 18:20
source share

Despite the title, the overloaded method actually didDeselectRowAtIndexPath , so it seems that the behavior is correct - when you touch the 1st line, the 0th one is canceled, and vice versa.

+3
Nov 07 '10 at 18:24
source share



All Articles