I am a beginner and maybe it is a trivial question. I have this method:
-(NSString *)getInfoFormediaItem:(MPMediaItemCollection *)list {
NSString *trackCount;
if ([list count] > 0) {
trackCount = [NSString stringWithFormat:NSLocalizedString(@"%lu Songs", @""), (unsigned long)[list count]];
} else if([list count] == 1) {
trackCount = [NSString stringWithFormat:NSLocalizedString(@"1 Song", @"")];
} else {
trackCount = [NSString stringWithFormat:NSLocalizedString(@"0 Song", @"") ];
}
return [NSString stringWithFormat:@"%@", trackCount];
}
I would like to call it here with a MPMediaItemCollection:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if( cell == nil )
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
}
MPMediaQuery *playlistsQuery = [MPMediaQuery playlistsQuery];
NSArray *playl = [playlistsQuery collections];
MPMediaItem *rowItem = [playl objectAtIndex:indexPath.row];
MPMediaItemCollection * collection = [[MPMediaItemCollection alloc] initWithItems:[NSArray arrayWithObject:rowItem]];
cell.detailTextLabel.text = [self getInfoFormediaItem:collection];
}
I would like to get the number of tracks in each playlist. It doesn't work. How do I fix? Thanks in advance!
Why are you using
performSelector:withObject:? Just call the method directly:Why are you passing
nilto thewithObject:parameter? That's why your code goes to theelse.listisnilso[list count]will always be0. You need to pass an actual instance of aMPMediaItemCollection.Why are you needlessly using
stringWithFormat:for the 1 and 0 count checks? Just do:Based on your updated question, your
cellForRowAtIndexPathcode isn't correct for the getting the media collection. Thecollectionsmethod returns an array ofMPMediaCollectionobjects, notMPMediaItemobjects. You need:Now you can use
collectionwhen you callgetInfoFormediaItem:.