3

コードは機能し、テーブルにセクションを入力しますが、欠点があります。ネイティブの音楽アプリと同じように、曲のタイトルの句読点と「The」プレフィックスをエスケープしません。

私がこれをどのように行えばよいかについてのガイダンスを本当にいただければ幸いです。

- (void)viewDidLoad
{
    [super viewDidLoad];
    MPMediaQuery *songQuery = [MPMediaQuery songsQuery];
    self.songsArray = [songQuery items];
    self.sectionedSongsArray = [self partitionObjects:self.songsArray collationStringSelector:@selector(title)];
}

- (NSArray *)partitionObjects:(NSArray *)array collationStringSelector:(SEL)selector
{
    UILocalizedIndexedCollation *collation = [UILocalizedIndexedCollation currentCollation];
    NSInteger sectionCount = [[collation sectionTitles] count];
    NSMutableArray *unsortedSections = [NSMutableArray arrayWithCapacity:sectionCount];
    for(int i = 0; i < sectionCount; i++)
    {
        [unsortedSections addObject:[NSMutableArray array]];
    }
    for (id object in array)
    {
        NSInteger index = [collation sectionForObject:object collationStringSelector:selector];
        [[unsortedSections objectAtIndex:index] addObject:object];
    }
    NSMutableArray *sections = [NSMutableArray arrayWithCapacity:sectionCount];
    for (NSMutableArray *section in unsortedSections)
    {
        [sections addObject:[collation sortedArrayFromArray:section collationStringSelector:selector]];
    }
    return sections;
}
4

2 に答える 2

5

私はこれを完全に見落としていました。ここでの答えは、単純に を使用することMPMediaQuerySectionです。Apple ドキュメントには理由があります。

于 2012-11-06T03:33:24.740 に答える
2

ココッチ -

音楽ライブラリ内のすべての曲を含むクエリのインデックスを作成するために使用した実装を次に示します。

MPMediaQuery *allSongsQuery = [MPMediaQuery songsQuery];

// Fill in the all songs array with all the songs in the user's media library
allSongsArray = [allSongsQuery items];

allSongsArraySections = [allSongsQuery itemSections];

allSongsArraySections は MPMediaQuerySection の NSArray であり、それぞれにタイトルと範囲があります。セクション 0 (私の場合は @"A" というタイトル) の NSArray オブジェクトの range.location は 0 で、range.length は 158 です。

UITableView に対して numberOfRowsInSection が呼び出されると、各セクションの range.length 値を返します。セクションの開始行として cellForRowAtIndexPath の range.location 値を使用し、allSongsArray から返す必要があるセルに到達するために indexPath.row を追加します。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
....
    // Return the number of rows in the section.
    MPMediaQuerySection *allSongsArraySection = globalMusicPlayerPtr.allSongsArraySections[section];
    return allSongsArraySection.range.length;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
...
    MPMediaQuerySection *allSongsArraySection = globalMusicPlayerPtr.allSongsArraySections[indexPath.section];
    rowItem = [globalMusicPlayerPtr.allSongsArray objectAtIndex:allSongsArraySection.range.location + indexPath.row];
....
}

これを使用する前に、ネイティブの音楽プレーヤーの実装を独自に作成して一致させようとしましたが、まったく同じように動作しませんでした。それだけでなく、ネイティブ インデックス作成は非常に高速です。

于 2013-03-29T08:35:30.773 に答える