在逻辑上(表关系)将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