Core Data淺談係列之五 : 在UITableView中展示
在邏輯上(表關係)將Team和Player關聯起來後,我們將其展現到UI視圖上。
首先,為App添加導航欄:
@interface AppDelegate : UIResponder <UIApplicationDelegate > @property (strong, nonatomic) UIWindow *window; @property (strong, nonatomic) UINavigationController *navController; @property (strong, nonatomic) ViewController *viewController; @end @implementation AppDelegate - (void)dealloc { [_window release]; [_navController release]; [_viewController release]; [super dealloc]; } - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; // Override point for customization after application launch. self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease]; self.navController = [[[UINavigationController alloc] initWithRootViewController:self.viewController] autorelease]; self.window.rootViewController = self.navController; [self.window makeKeyAndVisible]; return YES; }
然後在ViewController上添加一個UITableView,布局好並實現如下相應的代理函數:
#pragma mark - #pragma mark - UITableView DataSource - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [self.teamArray count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellIdentifier = @"TeamTableViewCellIdentifier"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; if (nil == cell) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier] autorelease]; } Team *teamObject = [self.teamArray objectAtIndex:indexPath.row]; UIImage *nbaImage = [UIImage imageNamed:@"nba@2x.jpg"]; cell.imageView.image = nbaImage; cell.imageView.backgroundColor = [UIColorredColor]; cell.textLabel.text = teamObject.name; cell.detailTextLabel.text = teamObject.city; cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; return cell; } #pragma mark - #pragma mark - UITableView Delegate - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:YES]; Team *teamObject = [self.teamArray objectAtIndex:indexPath.row]; PlayerListViewController *playerListVC = [[[PlayerListViewController alloc] init] autorelease]; playerListVC.team = teamObject; playerListVC.cdViewController = self; [self.navigationController pushViewController:playerListVC animated:YES]; }
在插入一些球隊信息後,可以得到如下效果(按球隊名稱排序):

- (NSArray *)fetchTeamList { NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; NSEntityDescription *teamEntity = [NSEntityDescription entityForName:@"Team" inManagedObjectContext:self.managedObjectContext]; [fetchRequest setEntity:teamEntity]; NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"name"ascending:YES]; [fetchRequest setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]]; NSError *error = NULL; NSArray *array = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error]; if (error) { NSLog(@"Error : %@\n", [error localizedDescription]); } [fetchRequest release], fetchRequest = nil; return array; }
點擊cell,就進入到該隊的球員列表:

- (NSArray *)fetchPlayerList { NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; NSEntityDescription *teamEntity = [NSEntityDescription entityForName:@"Player" inManagedObjectContext:self.cdViewController.managedObjectContext]; [fetchRequest setEntity:teamEntity]; NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"age"ascending:YES]; [fetchRequest setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]]; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"team == %@", self.team]; [fetchRequest setPredicate:predicate]; NSError *error = NULL; NSArray *array = [self.cdViewController.managedObjectContext executeFetchRequest:fetchRequest error:&error]; if (error) { NSLog(@"Error : %@\n", [error localizedDescription]); } [fetchRequest release], fetchRequest = nil; return array; }
通過導航欄右邊的Add按鈕來添加球員信息:

- (IBAction)addBtnDidClick:(id)sender { // We don't check the user input. Player *playerObject = [NSEntityDescription insertNewObjectForEntityForName:@"Player" inManagedObjectContext:self.cdViewController.managedObjectContext]; playerObject.name = self.nameTextField.text; playerObject.age = [NSNumber numberWithInteger:[self.ageTextField.text integerValue]]; playerObject.team = self.team; [self.cdViewController saveContext]; [self dismissModalViewControllerAnimated:YES]; } - (IBAction)cancelBtnDidClick:(id)sender { [self dismissModalViewControllerAnimated:YES]; }
以上對NSManagedObject的操作都位於同一份NSManagedObjectContext中。如上麵添加球員的函數addBtnDidClick:所注釋的,添加球員信息時並沒有對數據進行驗證 —— 這將在下一篇討論。
Brief Talk About Core Data Series, Part 5 : Showing in UITableView
最後更新:2017-04-04 07:03:39