2013-12-18 94 views
0

我有問題。我需要做些什麼來添加索引和部分到我的tableView?我正在使用核心數據。我有這樣的事情: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

它工作完美!但是這是一個問題......每個部分的標題不是名字的第一個字母,而是全名!我怎樣才能解決這個問題? –

+0

只需更改titleForHeaderInSection方法以僅返回字符串的第一個字母。如果有幫助,請接受答案! –

+0

非常感謝您的幫助! –