使用NSFetchedResultsController在节标题下重复行

时间:2016-05-12 15:28:50

标签: objective-c uitableview core-data nsfetchedresultscontroller uitableviewsectionheader

我太近了......但一定是错过了什么。

我的本​​地数据库有一个高尔夫球场列表,其中包含各种详细信息,包括州,州名(A,C,D等)的第一个字母...... NSFetchedResultsController正抓住课程列表,并且(或者是)工作得非常好,显示了我有记录的课程的所有州。

无论如何......部门负责人似乎正在工作......但是我的州现在正在重复每个州所拥有的课程数量。

下面的代码和屏幕截图。我错过了什么?!?它必须是如此明显的东西。

enter image description here

-(NSFetchedResultsController *)fetchedResultsController {
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription
                                   entityForName:@"Courses"
                            inManagedObjectContext:_managedObjectContext];

    request.entity = entity;

    request.sortDescriptors = [NSArray arrayWithObject:
                               [NSSortDescriptor
                                sortDescriptorWithKey:@"locState"
                                ascending:YES
                            selector:@selector(caseInsensitiveCompare:)]];


    request.returnsDistinctResults = YES;
    request.fetchBatchSize = 20;

    NSFetchedResultsController *frc = [[NSFetchedResultsController alloc]
                                       initWithFetchRequest:request
                            managedObjectContext:self.managedObjectContext
                                sectionNameKeyPath:@"locStateSectionHead"
                                       cacheName:nil];
    frc.delegate = self;

    NSError *error = nil;
    if (![frc performFetch:&error]) {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }

    self.fetchedResultsController = frc;
    return _fetchedResultsController;
}

然后是tableView设置的其余部分:

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

- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section {

    if ([[_fetchedResultsController sections] count] > 0) {
        id <NSFetchedResultsSectionInfo> sectionInfo = [[_fetchedResultsController sections] objectAtIndex:section];
        return [sectionInfo numberOfObjects];
    } else {
        return 0;
    }
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
    return [sectionInfo name];
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    return [self.fetchedResultsController sectionIndexTitles];
}

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
    return [self.fetchedResultsController sectionForSectionIndexTitle:title atIndex:index];
}

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"stateCell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }

    // Configure the cell...
    Courses *course_info = [_fetchedResultsController objectAtIndexPath:indexPath];
    cell.textLabel.text = course_info.locState;

    return cell;
}

1 个答案:

答案 0 :(得分:1)

假设您希望表视图显示状态,那么您的NSFetchedResultsController应该获取State而不是Course。您正在获得重复,因为您正在从课程中排序,并且您在州内有多个课程。

配置NSFetchedResultsController以加载State实体,然后您的表视图将正确显示。当用户选择状态时,您可以使用状态与课程之间的关系来显示下一个场景。

你只需要倒退:)

相关问题