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);
}
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) {}
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];
}
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With