Ios UITableView节/每个“名称”都是自己的节
我需要你的帮助,这个问题快把我逼疯了!我试图按字母顺序排列UITableView行,但我得到的结果是,每一行都有自己的节,两个节的名称相同 我怎样才能解决这个问题 这是我的代码:Ios UITableView节/每个“名称”都是自己的节,ios,objective-c,uitableview,core-data,sections,Ios,Objective C,Uitableview,Core Data,Sections,我需要你的帮助,这个问题快把我逼疯了!我试图按字母顺序排列UITableView行,但我得到的结果是,每一行都有自己的节,两个节的名称相同 我怎样才能解决这个问题 这是我的代码: - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return [[self.fetchedResultsController sections] count]; } - (NSArray *)sectionIndexTit
- (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;
}
你确定这部分代码吗
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
return [self.fetchedResultsController sectionForSectionIndexTitle:title atIndex:index];
}
这将生成一个递归循环 已将“获取结果”控制器的sectionNameKeyPath参数设置为
@名称,因此FRC为每个名称创建一个表视图部分
如果不需要节,只需设置sectionNameKeyPath:nil
如果要根据首字母将名称分组到各个部分,
将sectionNameKeyPath设置为仅包含
每个对象的首字母,如此处所述:
基于NSFetchedResultController,这是正确的。我在这里没有看到递归循环。表视图数据源方法sectionForSectionIndexTitle调用获取的结果控制器方法sectionForSectionIndexTitle。