0
votes

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?

2
send the self [self performSegueWithIdentifier:@"bundleItemDetails" sender:self]; - said
Already tried that, same result, cell is nil - Shardon
so uo add self.tableview.datasource=self and self.tableview.delegate=self ? - said
yes, both are set to self - Shardon
tell me you used a NSMutableArray for filling your tableView ? - said

2 Answers

0
votes

Check if your tableView tableView.allowsSelection is YES

0
votes

You could try using properties to hold the values you want to pass in the segue. You would set the properties in didSelectRowAtIndexPath: and then access them in prepareForSegue:sender:.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    self.valueImage = cell.itemImage.image;
    // etc...

    [self performSegueWithIdentifier:@"bundleItemDetails" sender:self];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{
    if ([[segue identifier] isEqualToString:@"bundleItemDetails"]) {
        UINavigationController *navController = [segue destinationViewController];
        itemDetails *bundleProducts = (itemDetails*)([navController viewControllers][0]);

        bundleProducts.valueImage = self.valueImage;
        bundleProducts.valueName = self.valueName;
        bundleProducts.valueDescription = self.valueDescription;
        bundleProducts.detailFullPrice.text = self.detailFullPrice;
    }
}