2

我有一個表使用NSFetchedResultsController。這讓我與存在NSFetchedResultsController與索引UITableViewController和UILocalizedIndexedCollat​​ion

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

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
{ 
    return [[[_fetchedResultsController sections] objectAtIndex:section] name]; 
} 

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

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

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

頭的索引,但我想用UILocalizedIndexCollat​​ion以獲得完整的字母表。

如何將這些方法連接到NSFetchedResultsController?

我想我需要從目前的整理

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView 
{ 
    return [[UILocalizedIndexedCollation currentCollation] sectionIndexTitles]; 
} 

獲得指數冠軍,但我失去了對如何寫這個方法

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index 
{ 
    //How do I translate index here to be the index of the _fetchedResultsController? 
    return [_fetchedResultsController sectionForSectionIndexTitle:title atIndex:index]; 
}  
+0

檢查我的擴展答案:http://stackoverflow.com/a/15587961/1791090 – dimanitm 2013-03-23 14:43:43

回答

6

我認爲你必須遍歷獲取的成果例如:

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index 
{ 
    NSInteger section = 0; 
    for (id <NSFetchedResultsSectionInfo> sectionInfo in [_fetchedResultsController sections]) { 
     if ([sectionInfo.indexTitle compare:title] >= 0) 
      break; 
     section++; 
    } 
    return section; 
} 

對於沒有匹配部分的部分索引標題,您必須決定是否要跳轉到「較低」或「較高」部分。上面的方法跳轉到下一個更高的部分。

+0

改良馬丁的解決方案跳到前面,也處理情況的指標可能是像「#」 – 2013-02-18 03:25:25

1

我從Martin那裏得到的解決方案。

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index 
{ 
    NSInteger localizedIndex = [[UILocalizedIndexedCollation currentCollation] sectionForSectionIndexTitleAtIndex:index]; 
    NSArray *localizedIndexTitles = [[UILocalizedIndexedCollation currentCollation] sectionIndexTitles]; 
    for(int currentLocalizedIndex = localizedIndex; currentLocalizedIndex > 0; currentLocalizedIndex--) { 
     for(int frcIndex = 0; frcIndex < [[_fetchedResultsController sections] count]; frcIndex++) { 
      id<NSFetchedResultsSectionInfo> sectionInfo = [[_fetchedResultsController sections] objectAtIndex:frcIndex]; 
      NSString *indexTitle = sectionInfo.indexTitle; 
      if([indexTitle isEqualToString:[localizedIndexTitles objectAtIndex:currentLocalizedIndex]]) { 
       return frcIndex; 
      } 
     } 
    } 
    return 0; 
} 
+0

我試過你的,但是恢復到了Martin的原因,當你的代碼段中沒有字母時,你的代碼經常崩潰。 – 2013-05-02 07:39:34

相關問題