0

I have a table view. I want my app to work like the "Contacts" app on the iPhone. When you select a person in the app, it goes to a view that has the users name and phone number. and it doesnt matter which cell is selected, it always goes to the same view, but with different data. How can i achieve the same thing? I want one view controller and i want each cell to load certain data onto the presented view controller! And i would like to use NSUserDefaults (if possible). Any Help would be great, Thank You!

P.S. i know this is kind of a broad question, but im unsure where to find an answer i have searched and searched. Thanks again!

4

2 に答える 2

0

I want one view controller and i want each cell to load certain data onto the presented view controller!

No problem. User taps on cell, your table's delegate gets a -tableView:didSelectRowAtIndexPath: message. Let's say the table's delegate and data source are both the view controller that manages the table (because that's a pretty common setup). So the view controller takes a look at the index path for the cell, grabs the associated data from wherever it keeps its data, instantiates the detail view controller with the data connected to the tapped cell, and pushes it.

The part that you're probably missing is some sort of model that stores the data displayed in the table. The view controller for the table needs to know about the model because it's the data source for the table. It already needs to know where to find enough data to configure each cell in the table. If you use that same model to store the detail data, you'll be able to use it to configure the detail view controller.

Example: Let's say you have a table with just one section. A simple model for that table could be an array of dictionaries. When your table view controller needs to populate a cell, it uses the row of the index path as an index into the array to get a dictionary, and it uses that to set up the cell. Exactly the same thing happens when the user taps a cell: you use the row from the index path to get the right dictionary from the array, and you use that to set up the detail view controller. You could even just pass the whole dictionary to the detail view controller and let the detail view controller get what it needs. That way the table view controller doesn't have to worry about exactly which details the detail view controller displays.

于 2012-10-31T02:53:11.230 に答える
0

You create an array of names (strings) and display those names in your table view. Then once a name was selected you save it in NSUserDefaults as a string.

- (void) tableView: (UITableView *) tableView didSelectRowAtIndexPath: (NSIndexPath *) indexPath
{
     NSString *selectedString = [namesArray objectAtIndex:[indexPath row]];
     NSUserDefaults *infoSaved = [NSUserDefaults standardUserDefaults];
     [infoSaved setObject:selectedString forKey:@"theName"];
     [infoSaved synchronize];
     [self performSegueWithIdentifier:@"segueToMoreInfo" sender:self];
}

Then at your second view controller you load the information from an NSDictionary that contains NSDictionary items.

-(void) loadData {
     NSUserDefaults *infoSaved = [NSUserDefaults standardUserDefaults];
     NSString *theName = [infoSaved objectForKey:@"theName"];
     NSDictionary *currentNameToDisplay = [itemsDictionary objectForKey:theName];
     // then you can set your labels with information in that NSDictionary item
     [nameLabel setString:[currentNameToDisplay objectForKey:@"userName"];
     [ageLabel  setString:[currentNameToDisplay objectForKey:@"userAge];
}

That would be a simple basic structure. You can save the main Names NSDictionary that contains the items into a plist file or even better save the dictionary in NSUserDefaults.

Hope that helps.

于 2012-10-31T04:50:47.893 に答える