push plist from UITableViewCell into UIView
我有一个plist,它会通过键" \\ Title "来编译UITableView,该plist在下面。Table View正确填充,因此我用以下方法推送了一个视图控制器:
1 2 3 4 5 6 7 | - (void) tableView: (UITableView*) tableView didSelectRowAtIndexPath: (NSIndexPath*) indexPath; { LocationsReviewViewController *nextViewController = [[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]; } |
如何获取上面的
这是nextViewController当前的ViewDidLoad外观(描述为UILabel)。选择" LA Place 1 "行时,为什么不显示@ " 3001 Sunset ":
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | - (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"]]; } <dict> <key>Rows</key> <dict> <key>Subtree</key> <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> <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> <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> |
查看您的Data3.plist,我看到它是2个项的字典。
- 第一项是带有键的NSArray:行
- 第二项是带有键的NSString:标题
因此在您的
1 2 3 4 5 6 7 8 9 10 11 12 | - (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]]; } |
现在,如果要将数据传递到下一个控制器,则应在
在您的LocationsReviewViewController.h中:
1 2 3 4 5 | // declare an ivar NSDictionary *data; // declare property @property (nonatomic, retain) NSDictionary *data; |
在您的LocationsReviewViewController.m中:
1 | @synthesize data; |
分配LocationsReviewViewController对象时:
1 2 3 4 5 6 7 8 | - (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]; } |