iOS UITableView部分与fetchedResultsController混淆

use*_*523 10 core-data tableview nsfetchedresultscontroller ios

我有一个实体只在一个部分中显示在表视图中.该实体有两个属性,workoutNametrainingLevel.两者都是字符串类型.训练水平由3种类型组成:1,2,3(trainingLevel =(整数16或字符串类型?哪个是理想的?)我想将表分成三个部分,每个部分包含相应训练级别的条目.

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

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return self.workoutType.workouts.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell =
    [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                                  reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }


    WorkoutSet *workoutSet = [self.fetchedResultsController objectAtIndexPath:indexPath];


    cell.textLabel.text = workoutSet.workoutName;
    cell.detailTextLabel.text = [NSString stringWithFormat:@"(%d)", workoutSet.days.count];    
}

-(void)fetchWorkoutSets
{

    NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"WorkoutSet"];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"workoutType = %@", self.workoutType];

    NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"workoutName" ascending:YES];
    [fetchRequest setSortDescriptors:@[sortDescriptor]];
    [fetchRequest setPredicate:predicate];
    self.fetchedResultsController = [[NSFetchedResultsController alloc]
                                 initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext
                                 sectionNameKeyPath:nil cacheName:nil];

    NSError *error;
    if (![self.fetchedResultsController performFetch:&error])
    {
        NSLog(@"Fetch failed: %@", error);
    }
}
Run Code Online (Sandbox Code Playgroud)

我正在努力的是:

  • 如何通过获取训练级别为1或2或3的条目数来确定核心数据模型中每个部分的行数.
  • 如何通过获取正确的项来填充每个部分的行.
  • 如何为每个节标题赋予标题.

mem*_*ons 18

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

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

@property (nonatomic,strong)NSManagedObjectContext* managedObjectContext;
@property (nonatomic,retain)NSFetchedResultsController *fetchedResultsController;
Run Code Online (Sandbox Code Playgroud)

在您的fetchedResultsController媒体资源中,您可以使用sectionKeyNamePath以下部分设置获取的结果:

- (NSFetchedResultsController *)fetchedResultsController {

    if (_fetchedResultsController != nil) {
        return _fetchedResultsController;
    }

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription 
                                           entityForName:@"Workouts"
                                  inManagedObjectContext:managedObjectContext];
    [fetchRequest setEntity:entity];

    NSSortDescriptor *sort = [[NSSortDescriptor alloc]
        initWithKey:@"workoutName" ascending:NO];
    [fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];

    [fetchRequest setFetchBatchSize:20];

    NSFetchedResultsController *theFetchedResultsController =
        [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
            managedObjectContext:managedObjectContext 
              sectionNameKeyPath:@"trainingLevel"
                       cacheName:@"Root"];
    self.fetchedResultsController = theFetchedResultsController;
    _fetchedResultsController.delegate = self;

    return _fetchedResultsController;

}
Run Code Online (Sandbox Code Playgroud)

你最初的人口fetchedResultsController可以发生在你的身上-viewDidLoad:

- (void)viewDidLoad {
    [super viewDidLoad];

    NSError *error;
    if (![[self fetchedResultsController] performFetch:&error]) {
        // Update to handle the error appropriately.
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        exit(-1);  // Fail
    }
}
Run Code Online (Sandbox Code Playgroud)

然后,您将返回部分的数量和行数,如下所示:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [[self.fetchedResultsController sections] count];
}

- (NSInteger)tableView:(UITableView *)tableView 
 numberOfRowsInSection:(NSInteger)section
{
   id <NSFetchedResultsSectionInfo> sectionInfo = 
       [[[self fetchedResultsController] sections] objectAtIndex:section];

   return [sectionInfo numberOfObjects];        
}
Run Code Online (Sandbox Code Playgroud)

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

- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

   // init the cell
   // and whatever other setup needed

   WorkoutSet *workoutSet = 
      [self.fetchedResultsController objectAtIndexPath:indexPath];

   // configure the cell from the managedObject properties
}
Run Code Online (Sandbox Code Playgroud)