我是初学者,也许这是一个微不足道的问题。我有这个方法:
-(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
。
你根本不需要调用这个语句:
cell.detailTextLabel.text = [self performSelector:@selector(getInfoFormediaItem:) withObject:nil];
在您的"获取信息媒体项目"方法中。当你定义单元格时,你可以在"cellforrowataIndexPath"方法中这样做,就像这样调用:
cell.detailTextLabel.text = [self getInfoFormediaItem:This_Is_A_List_You_Wanna_Pass_To_The_Method];
你应该很高兴。
除了其他海报指出的问题外,你的 if 语句将无法按预期工作:
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", @"") ];
}
"if ...>0" 子句将首先匹配,然后再检查值 1,因为 1> 为 0。因此,"if ... == 1"永远不会被评估为真。您需要对该 if 语句重新排序:
if ([list count] == 1) {
trackCount = NSLocalizedString(@"1 Song", @"");
else if ([list count] > 0) {
trackCount = [NSString stringWithFormat:NSLocalizedString(@"%lu Songs",
@""), (unsigned long)[list count]];
}
else
{
trackCount = NSLocalizedString(@"0 Songs", @"");
}