2013-12-18 54 views
0

我需要你的帮助,这个问题让我发疯!我试图让我的UITableView行按字母顺序排列,但我得到的是,每一行都有它自己的部分,两者都命名相同。UITableView部分/每个“名称”是它自己的部分

我该如何解决这个问题?

这是我的代码:

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

- (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]; 
} 

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

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 

    id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section]; 
    return [sectionInfo name]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"ListCell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    NSManagedObject *aPerson = [self.fetchedResultsController objectAtIndexPath:indexPath]; 

    cell.textLabel.text = [aPerson valueForKey:@"name"]; 
    cell.detailTextLabel.text = [aPerson valueForKey:@"number"]; 

    return cell; 
} 

// Override to support editing the table view. 
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSManagedObjectContext *managedObjectContext=[self.fetchedResultsController managedObjectContext]; 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     // Delete the row from the data source 
     [managedObjectContext deleteObject:[self.fetchedResultsController objectAtIndexPath:indexPath]]; 

     NSError *error; 
     if (![managedObjectContext save:&error]) { 
      UIAlertView *alert=[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Error saving after delete", @"Error saving after delete.") 
                 message:[NSString stringWithFormat:NSLocalizedString(@"Error was: %@, quitting.", @"Error was: %@,quitting."), [error localizedDescription]] 
                delegate:self 
              cancelButtonTitle:NSLocalizedString(@"Aw, Nuts", @"Aw, Nuts") 
              otherButtonTitles:nil]; 
      [alert show]; 
     } 
    } 
    else if (editingStyle == UITableViewCellEditingStyleInsert) { 
     // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view 
    } 
} 

#pragma mark - Table view delegate 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSManagedObject *selectedHero = [self.fetchedResultsController objectAtIndexPath:indexPath]; 
    [self performSegueWithIdentifier:@"DetailSegue" sender:selectedHero]; 
} 

#pragma mark - FetchedResultsController Property 

- (NSFetchedResultsController *)fetchedResultsController 
{ 
    if (_fetchedResultsController != nil) { 
     return _fetchedResultsController; 
    } 

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 
    NSManagedObjectContext *managedObjectContext = [appDelegate managedObjectContext]; 
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Team" 
              inManagedObjectContext:managedObjectContext]; 
    [fetchRequest setEntity:entity]; 
    [fetchRequest setFetchBatchSize:20]; 

    NSString *sectionKey = nil; 

    NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES]; 
    NSSortDescriptor *sortDescriptor2 = [[NSSortDescriptor alloc] initWithKey:@"number" ascending:YES]; 
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor1, sortDescriptor2, nil]; 
    [fetchRequest setSortDescriptors:sortDescriptors]; 
    sectionKey = @"name"; 

    _fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:sectionKey cacheName:@"Team"]; 
    _fetchedResultsController.delegate = self; 

    return _fetchedResultsController; 
} 

回答

0

你确定这部分代码吗?

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

这产生一个递归循环。

+0

你有什么建议? –

+0

基于NSFetchedResultController(https://developer.apple.com/library/ios/documentation/CoreData/Reference/NSFetchedResultsController_Class/Reference/Reference.html),它是正确的。 – Joshua

+0

这里我没有看到递归循环。表视图数据源方法'sectionForSectionIndexTitle'调用获取的结果控制器方法'sectionForSectionIndexTitle'。 –

0

您已将fetched results controller的参数sectionNameKeyPath设置为 @"name",因此FRC会为每个名称创建一个表视图部分。

如果你不想要部分,只需设置sectionNameKeyPath:nil

如果根据初始信要分组为部分的名称,设置 到sectionNameKeyPath只包含 每个对象的初始信实体的(瞬时)属性,如例如描述这里: How to use the first character as a section name