I have a plist that compiles a UITableView by the key "Title", the plist is below.The Table View populates correctly, I push a view controller with this:
- (void) tableView: (UITableView*) tableView didSelectRowAtIndexPath: (NSIndexPath*) indexPath;
{
LocationsReviewViewController *next开发者_StackOverflowViewController = [[LocationsReviewViewController alloc]init];
NSDictionary *rowData = [[[tableData objectAtIndex: indexPath.section] objectForKey: @"Rows"] objectAtIndex: indexPath.row];
nextViewController.title = [rowData objectForKey: @"Title"];
[self.navigationController pushViewController: nextViewController animated: YES];
}
How do I get nextViewController
above to retain some information about which object in the table was selected? In addition, how do I show the strings contained in each subtree key on the new nextViewController
view? What should that ViewDidLoad look like?
Here's what it the ViewDidLoad currently looks like for the nextViewController (description is a UILabel). Why doesn't show @"3001 Sunset" when the "LA Place 1" row is selected:
- (void)viewDidLoad
{
[super viewDidLoad];
NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Data3" ofType:@"plist"]];
NSDictionary *subtree = [dictionary objectForKey: @"Subtree"];
description.text = [NSString stringWithFormat:@"%@",[subtree objectForKey:@"Item 2"]];
}
<array>
<dict>
<key>Rows</key>
<array>
<dict>
<key>Subtree</key>
<array>
<string>Los Angeles, CA</string>
<string>Tuesday</string>
<string>3001 Sunset</string>
</array>
<key>Title</key>
<string>LA Place 1</string>
</dict>
<dict>
<key>Subtree</key>
<array>
<string>New York, NY</string>
<string>Sunday</string>
<string>1400 Broadway</string>
</array>
<key>Title</key>
<string>NYC Place 1</string>
</dict>
<dict>
<key>Subtree</key>
<array>
<string>Austin, TX</string>
<string>Sunday</string>
<string>2400 Lamar Blvd</string>
</array>
<key>Title</key>
<string>Austin Place 1</string>
</dict>
</array>
<key>Title</key>
<string>Section1</string>
</dict>
</array>
</plist>
Looking at your Data3.plist I see it's a dictionary of 2 items.
- First item is NSArray with a key: Rows
- Second item is a NSString with a key: Title
So in your - (void)viewDidLoad
your code is incorrect. This should be the correct code:
- (void)viewDidLoad {
[super viewDidLoad];
NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Data3" ofType:@"plist"]];
// get the Rows object
NSArray *rows = [dictionary objectForKey:@"Rows"];
// The array object contains 3 NSDictionary
NSDictionary *firstItem = [rows objectAtIndex:0]; // I am just getting the first item
// once I have the dictionary, which have 2 items, Subtree & title
NSArray *subtree = [firstItem objectForKey: @"Subtree"];
// subtree object is NSArray with 3 NSString object, therefore you have to use index
description.text = [NSString stringWithFormat:@"%@",[subtree objectAtIndex:2]];
}
Now, if you want to pass data to the next controller you should add a property to your LocationsReviewViewController
object.
In your LocationsReviewViewController.h:
// declare an ivar
NSDictionary *data;
// declare property
@property (nonatomic, retain) NSDictionary *data;
In your LocationsReviewViewController.m:
@synthesize data;
When you alloc the LocationsReviewViewController object:
- (void) tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath;
{
NSDictionary *rowData = [[[tableData objectAtIndex: indexPath.section] objectForKey: @"Rows"] objectAtIndex: indexPath.row];
LocationsReviewViewController *nextViewController = [[LocationsReviewViewController alloc]init];
nextViewController.data = rowData; // pass in your dictionary to your controller
nextViewController.title = [rowData objectForKey: @"Title"];
[self.navigationController pushViewController: nextViewController animated: YES];
}
精彩评论