2012-03-17 10 views
3

代碼工作並用表格填充表格,但它有一個缺陷:它不會在標題中跳出標點和'The'前綴,只是比如原生音樂應用程序的功能。整理MPMediaQuery時在歌名中跳出標點符號和'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; 
} 

回答

5

我完全忽略了這一點。這裏的答案是簡單地使用MPMediaQuerySection。 Apple文檔是有原因的!

+0

你能分享一下你是如何實現這個的嗎?我被困在同一件事上,我要瘋了!它是否適用於其他MPMediaItems/MPMediaCollections的列表?謝謝 – topLayoutGuide 2013-02-07 13:30:11

+0

我用分區對象方法編寫了我的整個媒體庫。這在某種程度上「很快」,但這對於MPMediaQuerySection來說更快。一些幫助將不勝感激! – topLayoutGuide 2013-02-08 12:35:06

2

Cocotutch -

下面是我對指數使用包含所有歌曲的查詢在我的音樂庫中的實現:

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,每個都有標題和範圍。部分零的NSArray對象(在我的情況下,標題@「A」)的range.location爲0,range.length爲158。 UITableView的。我使用cellForRowAtIndexPath中的range.location值作爲該部分的起始行,然後將indexPath.row添加到它以便到達我需要從我的allSongsArray返回的單元格。

- (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 2013-03-29 23:28:15

+0

這真是太棒了!ありがとうございます! – topLayoutGuide 2013-07-25 05:39:00