2013-12-18 4 views
0

У меня вопрос. Что мне нужно сделать, чтобы добавить индекс и разделы в мой TableView? Я использую Core Data. У меня есть что-то вроде этого:UITableView Индекс и разделы

- (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

не могли бы понять, что вы намерены делать на основе вышеприведенного публикуемую кода? – ldindu

ответ

0

Ваш NSFetchedResultsController имеют участки с объектами на него, вы просто бессмыслица, чтобы Asign источник данных вашего представления таблицы в классе и сделать это:

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

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



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = <#Get the cell#>; 
    NSManagedObject *managedObject = [<#Fetched results controller#> objectAtIndexPath:indexPath]; 

    // Configure the cell with data from the managed object. 
    return cell; 

} 



- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 
    if ([[self.fetchedResultsController sections] count] > 0) { 
     id <NSFetchedResultsSectionInfo> sectionInfo = [[<#Fetched results controller#> sections] objectAtIndex:section]; 
     return [sectionInfo name]; 
    } else 
     return nil; 
} 


- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView { 
    return [<#Fetched results controller#> sectionIndexTitles]; 
} 


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

Это принято от:

NSFetchedResulstController Class Reference

+0

Это сработало отлично! Но theres проблема ... Название каждого раздела это не первая буква имени, а все имя! Как я могу это исправить? –

+0

Просто измените метод titleForHeaderInSection, чтобы вернуть только первую букву строки. Пожалуйста, примите ответ, если это поможет! –

+0

Большое спасибо за вашу помощь! –

Смежные вопросы