I have trouble to make something work:
I have a UIViewController with a tableView in it that has been connected and had it's delegates set. I am trying to send a segue from didSelectRowatIndexPath and in prepareforSegue I am trying to read some of the selected cell's data (I am using the model to retrieve the actual -important- data, but I have some elements i.e. an imageView which I want to read from the cell)
What confuses me is no matter how I set the sender (self.tableView or self or indexPath or anything), in the prepareforSegue I always get the cell as nil.
If I change the UIViewController to a UITableViewController and make the sender as tableView, everything is working perfectly fine.
What am I missing?
Code Follows:
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self performSegueWithIdentifier:@"bundleItemDetails" sender:tableView];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:@"bundleItemDetails"])
{
NSIndexPath *selectedIndexPath = [self.tableView indexPathForSelectedRow];
itemCell *cell = (itemCell*)[self.tableView cellForRowAtIndexPath:selectedIndexPath];
NSDictionary *currentProduct = [[[productsArray objectAtIndex:selectedIndexPath.section] objectForKey:@"products"] objectAtIndex:selectedIndexPath.row];
UINavigationController *navController = [segue destinationViewController];
itemDetails *bundleProducts = (itemDetails*)([navController viewControllers][0]);
UIImage *img = cell.itemImage.image;
bundleProducts.valueImage = img;
bundleProducts.valueName = [currentProduct valueForKey:@"title"];
bundleProducts.valueDescription = [currentProduct valueForKey:@"description"];
bundleProducts.detailFullPrice.text = [currentProduct valueForKey:@"price"];
}
}
This Works perfectly with UITableViewController and returns nil for cell values for UIViewController
Update: Some new findings
It looks that indexPathForSelectedRow returns null and 0. In case I change the code of didSelectRowAtIndexPath to:
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self.tableView selectRowAtIndexPath:indexPath
animated:NO
scrollPosition:UITableViewScrollPositionNone];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
[cell setSelected:YES];
[self performSegueWithIdentifier:@"bundleItemDetails" sender:self.tableView];
}
I get the index values but I still have empty itemCell (itemCell is a custom cell with an external .xib, could this need to be called differently in a UIViewController than in a UITableviewController?