Jas*_*ker 2 objective-c nsfetchedresultscontroller uilocalizedcollation
我有一个使用NSFetchedResultsController的表.这会给我一个带有标题的索引
- (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];
}
Run Code Online (Sandbox Code Playgroud)
但我想使用UILocalizedIndexCollation来获得完整的字母表.
如何将这些方法连接到NSFetchedResultsController?
我想我需要从当前的Collation中获取索引标题
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return [[UILocalizedIndexedCollation currentCollation] sectionIndexTitles];
}
Run Code Online (Sandbox Code Playgroud)
但我对如何编写这种方法感到很遗憾
- (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];
}
Run Code Online (Sandbox Code Playgroud)
我认为你必须遍历获取的结果控制器部分并找到给定标题的匹配部分,例如:
- (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;
}
Run Code Online (Sandbox Code Playgroud)
对于没有匹配部分的部分索引标题,您必须决定是否要跳转到"较低"或"较高"部分.上面的方法跳转到下一个更高的部分.
| 归档时间: |
|
| 查看次数: |
1551 次 |
| 最近记录: |