2012-03-17 6 views
3

を照合する場合、句読点、および曲のタイトルに「」プレフィックスを脱出し、セクションを持つテーブルを移入し、それが欠陥を持っている:それはちょうど、句読点や曲のタイトルに「」プレフィックスをエスケープしません。ネイティブ音楽アプリの仕組みと同じように。は、コードの作品MPMediaQuery

は本当に私はこれをやって行くべきかにいくつかの指導をお願い申し上げます。

- (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; 
} 

答えて

5

私はこれを完全に見落としました。ここでの答えは、単にMPMediaQuerySectionを使用することです。 Appleのドキュメントには理由があります。タイトルを持っており、それぞれのMPMediaQuerySectionのNSArrayの、ある

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: - Cocotutch

+0

あなたはこれを実装する方法共有してもらえますか?私は同じことにこだわっていて、私は夢中になる!そして、それは他のMPMediaItems/MPMediaCollectionsのリストのために働くのでしょうか?おかげ – topLayoutGuide

+0

私はpartitionObjects方法で私の全体のメディアライブラリを書きました。これは、ある程度まで「迅速」ですが、これはMPMediaQuerySectionとLOT高速です。助けていただければ幸いです! – topLayoutGuide

2

ここ

は、私は私の音楽ライブラリ内のインデックスにすべての曲を含むクエリを使用し、実装です範囲。セクション0のNSArrayオブジェクト(私の場合は「A」というタイトル)は、range.locationが0、range.lengthが158です。

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

- (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]; 
.... 
} 

これを使用する前に私は自分自身を書き込むことによって、ネイティブの音楽プレーヤーの実装に一致するようにしようとした、そしてそれは非常に同じように動作しませんでした。それだけでなく、ネイティブの索引付けは非常に高速です!

+1

'MPMediaQuerySection'ルートの唯一の警告は、外国語をサポートしていないことです(英語がデフォルト言語でなく、音楽ライブラリに外国語の情報が含まれているデバイスの場合)。したがって、索引キー/セクションのタイトルは '# 'でフラッディングされ、テーブルは順序付けられていません。これは、 'partitionObjects'メソッドが勝つところです。 – sooper

+0

これは素晴らしいです!ありがとうございます!! – topLayoutGuide

関連する問題