如何在iOS中按字母顺序对MediaQuery的artistQuery数组进行排序

时间:2013-01-29 18:11:02

标签: ios cocoa-touch uitableview nsarray

在我的应用中,我加载艺术家并输入arrayOfArtists并显示在UITableView

所以我需要索引到我的UITableView。这就是为什么我需要像iOS内置音乐应用程序那样按字母顺序排列我的艺术家数组。

enter image description here

与上面的照片一样,它可以使用UITableView索引Title

所以我需要按字母顺序对Artist Array进行排序。

我该怎么做?

以下是我从iPodLibrary加载Artist的一些代码。

ViewDidLoad

中的

    MPMediaQuery *query = [MPMediaQuery artistsQuery];
    query.groupingType = MPMediaGroupingAlbumArtist;
    self.arrayOfArtist = [query collections];

CellForRowAtIndexPath

cell.textLabel.text = [NSString stringWithFormat:@"%@",[[[self.arrayOfArtist objectAtIndex:indexPath.row] representativeItem] valueForProperty:MPMediaItemPropertyArtist]];
sectionForSectionIndexTitle

中的

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
        return [self.arrayOfArtist count];
}

我希望TitleHeader按字母UITableView字母标记我的UITableView索引。 我怎样才能做到这一点? 谢谢你们。

2 个答案:

答案 0 :(得分:3)

按字母顺序使用选择器排序:

NSArray *sortedArray=[self.arrayOfArtist sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];

答案 1 :(得分:1)

X-Developer-X -

通过实施

,我得到了与你想要的几乎相同的东西
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

sectionIndexTitlesForTableView在屏幕的右边缘生成索引字符,titleForHeaderInSection生成各节之间的标题。我附上了结果的截屏。我确信通过更改表视图中的一些GUI设置可以使它完全相同。我看到的差异是我的标题栏有点透明,其中的字符是白色的。

A screen capture that reveals my unusual tastes in music:

相关问题