Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView didSelectRowAtIndexPath not being called on first tap

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);
}
like image 827
meggy.meng Avatar asked Feb 01 '12 14:02

meggy.meng


3 Answers

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) {}
like image 178
Jan Carlo Viray Avatar answered Nov 18 '22 09:11

Jan Carlo Viray


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];
}
like image 37
kpower Avatar answered Nov 18 '22 11:11

kpower


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"

like image 1
Jiraheta Avatar answered Nov 18 '22 10:11

Jiraheta