27
votes

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);
}
7
Can you post the code? Are you sure you haven't implemented didDeselectRowAtIndexPath instead?user467105
I didn't implement deselect. Thanks.meggy.meng
@AnnaKarenina - I hit this issue this morning, Anna Karenina has saved my skin again - thanks :)bodacious
is this issue solved? can you please answer (your own) the question, so we can close this!?Roger

7 Answers

34
votes

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) {}
1
votes

Please, try to change your -viewDidLoad to the code below (insert last line). Tell me about the results.

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView reloadData];
}
1
votes

I am not sure if this has been answered but I was running with this issue and just like "Anna Karenina" commented above. Make sure that you pay close attention to detail.

This is cause by you implementing the DEselect

-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath

This will cause the first click not to work but following clicks and any other cell will work as expected.

Solution: make sure you pay attention and use didSelectRowAtIndexPath

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

hope that helps and like I mention before this was solved by "Anna Karenina"

0
votes

Please Connect Delegate from Xib to Files Owners. Then Try it Will Work.

0
votes

Add this code in your viewDidLoad method

[self.tableView reloadData]
0
votes

I had a UIGestureRecognizer on the view inside of the storyboard. I had to remove it and it worked like normal.

0
votes

I also had this problem when passing data to another VC. I solved it by switching to a manual segue from the Table VC to the detail VC.

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        selectedData = data[indexPath.row]

        performSegue(withIdentifier: "Detail", sender: self)
}