Reputation: 113
I have a table view displaying a list of contacts and when you click a cell, a detail view controller pops up with labels showing the contact name, number, fax, and address. My problem is that every time I click a cell, the first value in the plist pops up no matter which cell i click. I found my error in indexPath when NSLog(@"%@",indexPath);
returned null
everytime. I think the problem is in this method but it looks the same in other classes and works fine.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"showCountyInfo"])
{
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSLog(@"%@",indexPath);
TSPBookCoverViewController *destViewController = segue.destinationViewController;
destViewController.countyName = [[self.books objectAtIndex:indexPath.row] objectForKey:@"Name"];//error
destViewController.phoneName = [[self.books objectAtIndex:indexPath.row] objectForKey:@"Phone"];
destViewController.faxName = [[self.books objectAtIndex:indexPath.row] objectForKey:@"Fax"];
destViewController.addressName = [[self.books objectAtIndex:indexPath.row] objectForKey:@"Address"];
}
}
When I enter integers instead of indexPath.row
, it works like it should, so the problem is here I just can't find it. Sorry if it's obvious I've tried looking for awhile now!
Edit: Here is didSelectRowAtIndexPath method:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
// Perform Segue
[self performSegueWithIdentifier:@"showCountyInfo" sender:self];
}
Upvotes: 1
Views: 157
Reputation: 107121
The issue is with the following code:
[tableView deselectRowAtIndexPath:indexPath animated:YES];
You are deselecting the cell inside the didSelectRowAtIndexPath:
method. So the indexPathForSelectedRow
will always return nil
Upvotes: 2
Reputation: 2185
Best way for you to resolve it is to get the Dictionary from self.books array in
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
and then perform the segue and get values from that dictionary in prepareForSegue
Upvotes: 0