0

I have an array of objects that have multiple instance variables (name, address, number, etc). I also have a UITableViewController whose table view is populated with the name variable of each of these objects.

I have also created a DetailViewController that should display the rest of the information held by these objects when that objects' designated cell is selected with tableView:didSelectRowAtIndexPath:. The problem is, these cells only have references to the cell's objects' name variable.

How should I go about fixing this problem? Would I need to subclass the cell so that I could give each cell a reference to the entire object? or is there an easier way?

This is my current tableView:cellForRowAtIndexPath: method:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if(!cell){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
    }
    cell.textLabel.text = [[listings objectAtIndex:indexPath.row] objectForKey:@"listingName"];
    return cell;
}

update: it just occured to me that I could grab the row number from indexPath and grab the designated object from the array. Would this be more viable?

2
  • Subclass it and declare a property? Commented Aug 2, 2013 at 20:22
  • You certainly don't subclass the cell to get a reference to the entire object -- cells are for displaying data, not for supplying data. Your final sentence mentions the way to go. Commented Aug 3, 2013 at 1:43

2 Answers 2

4

In tableView:didSelectRowAtIndexPath: you can just use [listings objectAtIndex:indexPath.row] to get the selected object, and pass that to the detail view controller.

(You should never try to use the table view cells as data source.)

Sign up to request clarification or add additional context in comments.

1 Comment

and if you really want to go all nu skool you could do listings[indexPath.row]
2

Since you are displaying the listings directly from their array position, you get them out again the same way once you pressed the row.

In tableView:didSelectRowAtIndexPath: just do [listings objectAtIndex:indexPath.row] to get the object in question and send it over to the DetailViewController

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.