在数字化转型中,选择合适的跨平台开发框架不仅能提高效率,还有助于确保数据安全与合规性。
640
2022-10-04
Core Data浅谈系列之五 : 在UITableView中展示
在逻辑上(表关系)将Team和Player关联起来后,我们将其展现到UI视图上。
首先,为App添加导航栏:
[cpp] view plain copy 1. @interface AppDelegate : UIResponder
然后在ViewController上添加一个UITableView,布局好并实现如下相应的代理函数:
[cpp] view plain copy 1. #pragma mark -2. #pragma mark - UITableView DataSource3. 4. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView5. {6. return 1;7. }8. 9. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section10. {11. return [self.teamArray count];12. }13. 14. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath15. {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 Delegate35. 36. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath37. {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 *)fetchTeamList2. {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. }
[cpp] view plain copy 1. - (NSArray *)fetchPlayerList2. {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)sender2. {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)sender14. {15. [self dismissModalViewControllerAnimated:YES];16. }
以上对NSManagedObject的操作都位于同一份NSManagedObjectContext中。如上面添加球员的函数addBtnDidClick:所注释的,添加球员信息时并没有对数据进行验证 —— 这将在下一篇讨论。
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~