Christian Gossain
Christian Gossain

Reputation: 5972

When a row is selected in a table, how can I inform the detail view about which row was selected?

In my app I am using a navigation controller that uses a table view to organize data. Now when the user selects a row a detail view pushed onto the stack. This detail view is the same no matter which row is selected. The only difference is that depending on which row was selected, my detail view will load data corresponding to the selected row from a saved .plist file.

So I was wondering how can I inform my detail view about which row was selected in my table view.

Upvotes: 1

Views: 178

Answers (3)

Christian Gossain
Christian Gossain

Reputation: 5972

Actually it was as simple as adding an NSUInteger ivar in the detail view controller and then simply setting its value from the tables delegate method just before I push the view onto the stack. Thanks for your answers guys.

Upvotes: 1

Chris
Chris

Reputation: 1475

Within your table view controller, the didSelectRowAtIndexPath: method will be called when the user selects a row. The returned value indexPath provides the section and row numbers of the selected row. From that, you can work out the item in question (e.g. its the nth in the array you've got them from) and that can be passed to the detail view controller.

Upvotes: 1

Mick Walker
Mick Walker

Reputation: 3847

Can you not simply just create a custom init method in the view controller? For example, I pass a selected date to a ViewController using the following:

.. interface

- (id)initWithNibNameandDate:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil  dateValue:(NSDate *) selectedDate;

... implementation

- (id)initWithNibNameandDate:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil  dateValue:(NSDate *) selectedDate {
    self.selDate = selectedDate;
    return [super initWithNibName:nibNameOrNil bundle:nil];
}

It really doesn't matter what data type you are passing over.

Hope this helps.

Upvotes: 2

Related Questions