我是初学者,也许这是一个微不足道的问题。 我有这个方法:
-(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];
}
我想在这里用 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];
}
我想获取每个播放列表中的轨道数。 它不起作用。我该如何解决?提前致谢!
最佳答案
为什么要使用
performSelector:withObject:
?直接调用方法即可:cell.detailTextLabel.text = [self getInfoFormediaItem:collection];
为什么要将
nil
传递给withObject:
参数?这就是您的代码转到else
的原因。list
为nil
,因此[list count]
将始终为0
。您需要传递MPMediaItemCollection
的实际实例。为什么您不必要地使用
stringWithFormat:
进行 1 和 0 计数检查?只是做:-(NSString *)getInfoFormediaItem:(MPMediaItemCollection *)list { NSString *trackCount; if ([list count] > 1) { trackCount = [NSString stringWithFormat:NSLocalizedString(@"%lu Songs", @""), (unsigned long)[list count]]; } else if([list count] == 1) { trackCount = NSLocalizedString(@"1 Song", @""); } else { trackCount = NSLocalizedString(@"0 Song", @""); } return trackCount; }
根据您更新后的问题,您的
cellForRowAtIndexPath
代码对于获取媒体集合是不正确的。collections
方法返回一组MPMediaCollection
对象,而不是MPMediaItem
对象。你需要:MPMediaQuery *playlistsQuery = [MPMediaQuery playlistsQuery]; NSArray *playl = [playlistsQuery collections]; MPMediaItemCollection *collection = playl[indexPath.row];
现在您可以在调用
getInfoFormediaItem:
时使用collection
。
关于ios - 使用多个参数调用 NSString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36752632/