0

I'm a complete noob when it comes to xcode (started learning 3 weeks ago), PHP is my thing. Anyways, I'm trying to pass a value from a MasterViewController to a DetailViewController when somebody taps on a cell from a tableview ((which is being populated with data from a json file). The problem is that I get a null value the first time. It works perfectly after that.

This is my cellForRowAtIndexPath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

FetchDetails* item = _feed.items[indexPath.row];

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

UILabel *itemTitle = (UILabel *)[cell viewWithTag:101];
itemTitle.text = [NSString stringWithFormat:@"%@", item.prodTitle];

UILabel *itemDate = (UILabel *)[cell viewWithTag:102];
itemDate.text = [NSString stringWithFormat:@"%@", item.prodDate];

UILabel *itemUser = (UILabel *)[cell viewWithTag:103];
itemUser.text = [NSString stringWithFormat:@"by %@", item.prodUser];

UIImageView *itemThumb = (UIImageView *)[cell viewWithTag:100];
itemThumb.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString: item.prodImage]]];
return cell;
}

The didSelectRowAtIndexPath. item.prodId is part of the json but not displayed on the prototype cell. This is the value I need on the detailviewcontroller

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
 FetchDetails* item = _feed.items[indexPath.row];
_selectedItem = [NSString stringWithFormat:@"%@", item.prodId];
}

And the prepareForSegue:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

if ([[segue identifier] isEqualToString:@"showItemDetail"]) {

    DetailViewController *destVieController = segue.destinationViewController;
    destVieController.passedValue = _selectedItem;
    NSLog(@"%@", _selectedItem);

  }
}

I came up with all of this code after going through a couple of tutorials. I have no idea why It doesn't work the first time and works perfectly on the second click and so on. Any help would be appreciated.

2
  • Now that I think about it, it's not the segue that's passing the null value. prepareForSegue is the one getting the null value from didSelectRowAtIndexPath. So what I have to figure out is why _selectedItem is getting null when the user taps the first time.
    – Drowned
    Feb 2, 2013 at 3:57
  • After further testing, the values being passed are all over the place. Tapping on the first cell give me 1, then I click on the next value and it sends 1 too. When I click the second cell again, I get the correct value (2). It seems like it's getting the correct value only the second time. The first time you click on an item, _selectedItem doesn't get changed.
    – Drowned
    Feb 2, 2013 at 4:06

1 Answer 1

2

Solved it. I had to stop using didSelectRowAtIndexpath and do everything in prepareForSegue.

This is the code I used on the segue:

    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:self.tableView.indexPathForSelectedRow];
    UILabel *label = (UILabel *)[cell viewWithTag:99];

    DetailViewController *detailVC = segue.destinationViewController;
    detailVC.passedId = [label text];

As you can see, I got the prodId from a tagged label using viewWithTag.

1
  • Thank you! I just ran into the same issue. You saved me some headaches. :)
    – TheTC
    Oct 31, 2014 at 20:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.