在逻辑上(表关系)将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:所注释的,添加球员信息时并没有对数据进行验证 —— 这将在下一篇讨论。
相关推荐
【iphone应用开发】iphone应用开发之六:UITableView的详细讲解(一)
在复杂数据模型的处理练习中,开发者将学习如何设计一个复杂的数据模型,并将其与UITableView的cellForRowAt:方法相结合,以展示不同的数据属性。这不仅要求开发者对数据有深入的理解,还要求他们能够熟练地处理数据...
swift学习控件篇:UITableview
iOS-UITableView学习UITableView的使用
UITableView、UITableView基本用法、UITableView详解,UITabelView常用方法
第五部分:如何处理UITableView中的行选择 第六部分:应用Property List强化你的Simple Table应用程序 第七部分:如何在Xcode 4.* 添加定制字体到iOS 应用程序中? 第八部分:如何在iOS App中添加启动画面? 第九...
http://blog.csdn.net/duxinfeng2010/article/category/1155790 有详细介绍
这是iPhone之UITableView入门的示例程序,具体参考: http://blog.csdn.net/htttw/article/details/7875462
一个简单的概念证明,用于将SwiftUI内容嵌入UITableView单元中。 贡献 贡献使开源社区成为了一个令人赞叹的学习,启发和创造场所。 您所做的任何贡献都将不胜感激。 分叉项目 创建您的Feature分支( git checkout -...
在 UITableView 和 MapView 之间创建视差效果,就像 FourSquare 中的视图一样。 SLAppDelegate提供了示例用法。 要求 SLParallaxController使用 ARC 并且需要 iOS 7.0+。 适用于 iPhone 和 iPad。 需要框架MapKit ...
iPhone UITableView在UITableViewCell中具有自动布局功能以创建动态高度 这段代码来自我的视频教程,该视频教程介绍了如何在iOS 7中为UITableViewCell对象设置自动布局。 有一些陷阱(很多)可以使事情正常进行。 ...
实现简单的UITableView对图片的显示。可以展示多张图片。
UITableView 拉动刷新(即底部刷新) —— 这是一个简单的“上拉刷新”方法的演示,当您到达表视图的末尾时,它会加载更多数据。 这将表视图中的最后一行用作动画“加载”。 这在。 这仅用于说明目的。 在 Xcode...
基于UITableView的简单单选或多选清单。 用法 let selectionList = SelectionList () selectionList. items = [ " One " , " Two " , " Three " , " Four " , " Five " ] selectionList. addTarget ( self , action...
为了保持这种解耦,没有“UITableView”子类,允许开发者将此行为添加为自己的“UITableView”子类的聚合,而无需创建中间类或直接在管理表委托和数据源的UIViewController中添加. 安装说明 将整个...
UITableView相关操作
第二十七部分:Core Data 持久化数据存储 第二十八部分: 使用 SQLite 数据库存储数据 第二十九部分:通用应用程序、图标文件和 App 调试 第三十部分:自劢布局(Auto Layout) 第三十一部分:应用集合视图...
集合表 该教程可以在这里阅读: :
第五部分:如何处理UITableView中的行选择 第六部分:应用Property List强化你的Simple Table应用程序 第七部分:如何在Xcode 4.* 添加定制字体到iOS 应用程序中? 第八部分:如何在iOS App中添加启动画面?