UITableView didSelectRowAtIndexPath not being called on first tap

meggy.meng picture meggy.meng · Feb 1, 2012 · Viewed 15.1k times · Source

I'm having an issue with UITableView's didSelectRowAtIndexPath of ios 5, which is correct in ios 4.

The first time I tap any row in the table, the method does not get called. Once I select another row, it call the didSelectRowAtIndexPath, but pass the last indexPath.

I've set tableView.delegate already, and it can run correctly in ios4.

MainView.xib
UITabBarController
     --UINavigationController
         --**UITableViewController**

Any suggestions?

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    cell.textLabel.text = [[NSString alloc]initWithFormat:@"%d",indexPath.row];
    return cell;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%d",indexPath.row);
}

Answer

Jan Carlo Viray picture Jan Carlo Viray · Nov 2, 2013

You may have implemented (notice the Deselect): didDeselectRowAtIndexPath

...hence the trigger upon selecting a new cell which deselects the first one, and logging indexPath as the first one as well.

SOLUTION: didSelectRowAtIndexPath

yeah, they look super similar, and it doesn't help that didDeselectRowAtIndexPath is the first method Xcode's autocomplete selects most of the time.

FULL CODE:

// right
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {}