我想在我的 tableview 上显示一个索引栏,其中我的所有歌曲都按字母顺序排序,那些外语歌曲和数字在 # 中,就像 iOS 中的 iPod 音乐一样。 我读取了我所有歌曲数组对象的第一个字符,排除重复项并将其附加为索引。 我如何过滤外国/非字母和数字?
这就是它的样子,丑陋。
会显示 iPod 之类的东西。
-(NSMutableArray *)updateSongSectionIndexWithArray:(NSArray*)songArray andSelector:(SEL)selector
{
NSArray *indexedArray = [self partitionObjects:songArray collationStringSelector:selector];
return [[NSMutableArray alloc]initWithArray:indexedArray];
}
-(NSArray *)partitionObjects:(NSArray *)array collationStringSelector:(SEL)selector
{
self.collation = [UILocalizedIndexedCollation currentCollation];
NSInteger sectionCount = [[self.collation sectionTitles] count];//section count is take from sectionTitles and not sectionIndexTitles
NSMutableArray *unsortedSections = [NSMutableArray arrayWithCapacity:sectionCount];
//create an array to hold the data for each section
for(int i = 0; i < sectionCount; i++)
{
[unsortedSections addObject:[NSMutableArray array]];
}
if ([self.catString isEqualToString:ARTISTS])
{
//put each object into a section
for (id object in array)
{
if (!object)
{
continue;
}
NSInteger index = [self.collation sectionForObject:object collationStringSelector:selector];
[[unsortedSections objectAtIndex:index] addObject:object];
}
}
else
{
for (id object in array)
{
NSInteger index = [self.collation sectionForObject:object collationStringSelector:selector];
[[unsortedSections objectAtIndex:index] addObject:object];
}
}
NSMutableArray *sections = [NSMutableArray arrayWithCapacity:sectionCount];
//sort each section
for (NSMutableArray *section in unsortedSections)
{
[sections addObject:[self.collation sortedArrayFromArray:section collationStringSelector:selector]];
}
return sections;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [[[UILocalizedIndexedCollation currentCollation] sectionTitles] count];
}
最佳答案
我为联系人应用程序做了类似的解决方案。我首先自己定义字母表,然后您可以将适合的项目添加到每个部分(比较不区分大小写的字母,比较字符集的数字)。列表填满后,您可以删除(或隐藏)没有条目的部分。
关于ios - 对于非英语歌曲,如何使用 # 显示看起来像 iPod 音乐的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20896491/