Escaping punctuation, and 'The' prefix in song titles when collating a MPMediaQuery
-
24-05-2021 - |
Question
The code works and populates the table with sections, but it has a flaw: It doesn't escape punctuation and 'The' prefixes in song titles, just like how the native music app does.
Would really appreciate some guidance on how I should go about doing this.
- (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;
}
Solution
I completely overlooked this. The answer here is to simply use MPMediaQuerySection
. The Apple docs are there for a reason!
OTHER TIPS
Cocotutch -
Here's the implementation that I used to index a query containing all the songs in my music library:
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 is an NSArray of MPMediaQuerySection, each of which has a title and a range. The NSArray object for section zero (with title @"A" in my case) has range.location of 0 and range.length of 158.
I return the range.length value for each section when numberOfRowsInSection is called for my UITableView. I use the range.location value in cellForRowAtIndexPath as the starting row of the section, and then add indexPath.row to it in order to arrive at the cell I need to return from my 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];
....
}
Before using this I had tried to match the native music player's implementation by writing my own, and it didn't behave quite identically. Not only that, but the native indexing is MUCH faster!