iOS UITableView部分与fetchedResultsController混淆

前端之家收集整理的这篇文章主要介绍了iOS UITableView部分与fetchedResultsController混淆前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在一个部分中有一个实体显示在表视图中.实体有两个属性,即workoutName和trainingLevel.两者都是字符串类型.培训级别包括3种类型:1,2,3(trainingLevel =(整数16或字符串类型?哪个是理想的?)我想将表分为三个部分,每个部分包含相应培训级别的条目.

我该怎么做?我目前使用的代码如下:

  1. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  2. {
  3. // Return the number of sections.
  4. return 1;
  5. }
  6.  
  7. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  8. {
  9. // Return the number of rows in the section.
  10. return self.workoutType.workouts.count;
  11. }
  12. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  13. {
  14. static NSString *CellIdentifier = @"Cell";
  15. UITableViewCell *cell =
  16. [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  17. if (cell == nil)
  18. {
  19. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
  20. reuseIdentifier:CellIdentifier];
  21. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  22. }
  23.  
  24.  
  25. WorkoutSet *workoutSet = [self.fetchedResultsController objectAtIndexPath:indexPath];
  26.  
  27.  
  28. cell.textLabel.text = workoutSet.workoutName;
  29. cell.detailTextLabel.text = [NSString stringWithFormat:@"(%d)",workoutSet.days.count];
  30. }
  31.  
  32. -(void)fetchWorkoutSets
  33. {
  34.  
  35. NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"WorkoutSet"];
  36. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"workoutType = %@",self.workoutType];
  37.  
  38. NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"workoutName" ascending:YES];
  39. [fetchRequest setSortDescriptors:@[sortDescriptor]];
  40. [fetchRequest setPredicate:predicate];
  41. self.fetchedResultsController = [[NSFetchedResultsController alloc]
  42. initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext
  43. sectionNameKeyPath:nil cacheName:nil];
  44.  
  45. NSError *error;
  46. if (![self.fetchedResultsController performFetch:&error])
  47. {
  48. NSLog(@"Fetch Failed: %@",error);
  49. }
  50. }

我正在努力的是:

>如何通过核心数据模型确定每个部分的行数,方法获取训练级别为1或2或3的条目数.
>如何通过获取正确的项目来填充每个部分的行.
>如何给每个段头标题.

解决方法

以下是使用fetchedResultsController的一个很好的教程: http://www.raywenderlich.com/999/core-data-tutorial-for-ios-how-to-use-nsfetchedresultscontroller

创建一些属性来保存你的上下文和提取

  1. @property (nonatomic,strong)NSManagedObjectContext* managedObjectContext;
  2. @property (nonatomic,retain)NSFetchedResultsController *fetchedResultsController;

在您的fetchedResultsController属性中,使用sectionKeyNamePath在部分中设置获取的结果:

  1. - (NSFetchedResultsController *)fetchedResultsController {
  2.  
  3. if (_fetchedResultsController != nil) {
  4. return _fetchedResultsController;
  5. }
  6.  
  7. NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
  8. NSEntityDescription *entity = [NSEntityDescription
  9. entityForName:@"Workouts"
  10. inManagedObjectContext:managedObjectContext];
  11. [fetchRequest setEntity:entity];
  12.  
  13. NSSortDescriptor *sort = [[NSSortDescriptor alloc]
  14. initWithKey:@"workoutName" ascending:NO];
  15. [fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];
  16.  
  17. [fetchRequest setFetchBatchSize:20];
  18.  
  19. NSFetchedResultsController *theFetchedResultsController =
  20. [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
  21. managedObjectContext:managedObjectContext
  22. sectionNameKeyPath:@"trainingLevel"
  23. cacheName:@"Root"];
  24. self.fetchedResultsController = theFetchedResultsController;
  25. _fetchedResultsController.delegate = self;
  26.  
  27. return _fetchedResultsController;
  28.  
  29. }

您的fetchedResultsController的初始人数可能发生在您的-viewDidLoad中:

  1. - (void)viewDidLoad {
  2. [super viewDidLoad];
  3.  
  4. NSError *error;
  5. if (![[self fetchedResultsController] performFetch:&error]) {
  6. // Update to handle the error appropriately.
  7. NSLog(@"Unresolved error %@,%@",error,[error userInfo]);
  8. exit(-1); // Fail
  9. }
  10. }

然后,您将返回以下部分的行数和行数:

  1. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  2. {
  3. return [[self.fetchedResultsController sections] count];
  4. }
  5.  
  6. - (NSInteger)tableView:(UITableView *)tableView
  7. numberOfRowsInSection:(NSInteger)section
  8. {
  9. id <NSFetchedResultsSectionInfo> sectionInfo =
  10. [[[self fetchedResultsController] sections] objectAtIndex:section];
  11.  
  12. return [sectionInfo numberOfObjects];
  13. }

然后,您可以获取特定行的托管对象,如下所示:

  1. - (UITableViewCell *)tableView:(UITableView *)tableView
  2. cellForRowAtIndexPath:(NSIndexPath *)indexPath
  3. {
  4.  
  5. // init the cell
  6. // and whatever other setup needed
  7.  
  8. WorkoutSet *workoutSet =
  9. [self.fetchedResultsController objectAtIndexPath:indexPath];
  10.  
  11. // configure the cell from the managedObject properties
  12. }

猜你在找的iOS相关文章