27

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);
}
JOM
  • 8,139
  • 6
  • 78
  • 111
meggy.meng
  • 271
  • 1
  • 4
  • 5
  • 29
    Can you post the code? Are you sure you haven't implemented didDeselectRowAtIndexPath instead? –  Feb 01 '12 at 14:21
  • I didn't implement deselect. Thanks. – meggy.meng Feb 01 '12 at 14:31
  • This should work no problem. Is this class a subclass of UITableViewController? If it is than there is no need to set delegate in viewDidLoad method cos it's already assigned. Otherwise add delegate in class .h file in brackets . – Borut Tomazin Feb 02 '12 at 07:48
  • 2
    @AnnaKarenina - I hit this issue this morning, Anna Karenina has saved my skin again - thanks :) – bodacious Mar 22 '12 at 15:11
  • @AnnaKarenina - Thank you! *So* tricky, especially since 'deselect' precedes 'select' alphabetically. – Timo Oct 23 '12 at 18:27
  • 2
    possible duplicate of [UITableView didSelectRowAtIndexPath: not being called on first tap](http://stackoverflow.com/questions/2106292/uitableview-didselectrowatindexpath-not-being-called-on-first-tap) – Donal Fellows Nov 15 '12 at 12:01
  • Being working with table views and controllers on daily basis and just faced the very same issue. Thanks to @AnnaKarenina I know the reason, did autocomplete with didDeselectRowAtIndexPath. – i4niac Nov 15 '12 at 12:44
  • 4
    is this issue solved? can you please answer (your own) the question, so we can close this!? – Roger Dec 04 '12 at 09:06
  • this work fine on my side and work on first tap and give correct ans – Waseem Shah Feb 15 '13 at 08:46
  • Another Gesture recognizer on the view May be the reason. – Sudheesh Apr 13 '16 at 11:40

7 Answers7

34

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) {}
Jan Carlo Viray
  • 11,856
  • 11
  • 42
  • 63
1

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];
}
kpower
  • 3,871
  • 4
  • 42
  • 62
1

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"

iBug
  • 2,334
  • 3
  • 32
  • 65
Jiraheta
  • 471
  • 7
  • 17
0

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

Jaha Rabari
  • 173
  • 8
0

Add this code in your viewDidLoad method

[self.tableView reloadData]
Dhara
  • 4,093
  • 2
  • 36
  • 69
vinay
  • 351
  • 1
  • 5
  • 16
0

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

tuvok
  • 679
  • 5
  • 16
0

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)
}
MikeJ
  • 2,367
  • 3
  • 18
  • 23
  • how that should solve it if the didselect method is being called at all ? please help me to understand more thanks :) – ahmedHegazi May 17 '17 at 08:56