在逻辑上(表关系)将Team和Player关联起来后,我们将其展现到UI视图上。
首先,为App添加导航栏:
[cpp]
view plain
copy
1. @interface AppDelegate : UIResponder <UIApplicationDelegate >
2.
3. @property (strong, nonatomic) UIWindow *window;
4. @property (strong, nonatomic) UINavigationController *navController;
5. @property (strong, nonatomic) ViewController *viewController;
6.
7. @end
8.
9.
10. @implementation AppDelegate
11.
12. - (void)dealloc
13. {
14. [_window release];
15. [_navController release];
16. [_viewController release];
17. [super dealloc];
18. }
19.
20. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
21. {
22. self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
23. // Override point for customization after application launch.
24. "ViewController" bundle:nil] autorelease];
25. self.navController = [[[UINavigationController alloc] initWithRootViewController:self.viewController] autorelease];
26. self.window.rootViewController = self.navController;
27. [self.window makeKeyAndVisible];
28. return YES;
29. }
然后在ViewController上添加一个UITableView,布局好并实现如下相应的代理函数:
[cpp]
view plain
copy
1. #pragma mark -
2. #pragma mark - UITableView DataSource
3.
4. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
5. {
6. return 1;
7. }
8.
9. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
10. {
11. return [self.teamArray count];
12. }
13.
14. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
15. {
16. static NSString *cellIdentifier = @"TeamTableViewCellIdentifier";
17. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
18. if (nil == cell) {
19. cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier] autorelease];
20. }
21.
22. Team *teamObject = [self.teamArray objectAtIndex:indexPath.row];
23. "nba@2x.jpg"];
24. cell.imageView.image = nbaImage;
25. cell.imageView.backgroundColor = [UIColorredColor];
26. cell.textLabel.text = teamObject.name;
27. cell.detailTextLabel.text = teamObject.city;
28. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
29.
30. return cell;
31. }
32.
33. #pragma mark -
34. #pragma mark - UITableView Delegate
35.
36. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
37. {
38. [tableView deselectRowAtIndexPath:indexPath animated:YES];
39.
40. Team *teamObject = [self.teamArray objectAtIndex:indexPath.row];
41. PlayerListViewController *playerListVC = [[[PlayerListViewController alloc] init] autorelease];
42. playerListVC.team = teamObject;
43. playerListVC.cdViewController = self;
44. [self.navigationController pushViewController:playerListVC animated:YES];
45. }
在插入一些球队信息后,可以得到如下效果(按球队名称排序):
[cpp]
view plain
copy
1. - (NSArray *)fetchTeamList
2. {
3. NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
4. "Team" inManagedObjectContext:self.managedObjectContext];
5. [fetchRequest setEntity:teamEntity];
6.
7. "name"ascending:YES];
8. [fetchRequest setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
9.
10. NSError *error = NULL;
11. NSArray *array = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
12. if (error) {
13. "Error : %@\n", [error localizedDescription]);
14. }
15.
16. [fetchRequest release], fetchRequest = nil;
17.
18. return array;
19. }
点击cell,就进入到该队的球员列表:
[cpp]
view plain
copy
1. - (NSArray *)fetchPlayerList
2. {
3. NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
4. "Player" inManagedObjectContext:self.cdViewController.managedObjectContext];
5. [fetchRequest setEntity:teamEntity];
6.
7. "age"ascending:YES];
8. [fetchRequest setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
9.
10. "team == %@", self.team];
11. [fetchRequest setPredicate:predicate];
12.
13. NSError *error = NULL;
14. NSArray *array = [self.cdViewController.managedObjectContext executeFetchRequest:fetchRequest error:&error];
15. if (error) {
16. "Error : %@\n", [error localizedDescription]);
17. }
18.
19. [fetchRequest release], fetchRequest = nil;
20.
21. return array;
22. }
通过导航栏右边的Add按钮来添加球员信息:
[cpp] view plain copy
1. - (IBAction)addBtnDidClick:(id)sender
2. {
3. // We don't check the user input.
4. "Player" inManagedObjectContext:self.cdViewController.managedObjectContext];
5. playerObject.name = self.nameTextField.text;
6. playerObject.age = [NSNumber numberWithInteger:[self.ageTextField.text integerValue]];
7. playerObject.team = self.team;
8. [self.cdViewController saveContext];
9. [self dismissModalViewControllerAnimated:YES];
10. }
11.
12.
13. - (IBAction)cancelBtnDidClick:(id)sender
14. {
15. [self dismissModalViewControllerAnimated:YES];
16. }
以上对NSManagedObject的操作都位于同一份NSManagedObjectContext中。如上面添加球员的函数addBtnDidClick:所注释的,添加球员信息时并没有对数据进行验证 —— 这将在下一篇讨论。