0

我是初学者,也许这是一个微不足道的问题。我有这个方法:

-(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];
}

我想获取每个播放列表中的曲目数。它不起作用。我该如何解决?提前致谢!

4

3 回答 3

5
  1. 你为什么用performSelector:withObject:?直接调用方法即可:

    cell.detailTextLabel.text = [self getInfoFormediaItem:collection];
    
  2. 你为什么要传递nil参数withObject:?这就是为什么您的代码转到else. 永远list都是nil这样。您需要传递 a 的实际实例。[list count]0MPMediaItemCollection

  3. 为什么您不必要地使用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;
    }
    
  4. 根据您更新的问题,您的cellForRowAtIndexPath代码对于获取媒体集合不正确。该collections方法返回一个对象数组MPMediaCollection,而不是MPMediaItem对象。你需要:

    MPMediaQuery *playlistsQuery = [MPMediaQuery playlistsQuery];
    NSArray *playl = [playlistsQuery collections];
    MPMediaItemCollection *collection = playl[indexPath.row];
    

    现在您可以collection在调用时使用getInfoFormediaItem:

于 2016-04-20T18:57:45.850 回答
0

您根本不需要调用此语句:

cell.detailTextLabel.text =  [self performSelector:@selector(getInfoFormediaItem:) withObject:nil];

在您的“getInfoFormmediaItem”方法中。当您定义单元格时,您可以在“cellforrowataIndexPath”方法中执行此操作,只需像这样调用:

cell.detailTextLabel.text = [self getInfoFormediaItem:This_Is_A_List_You_Wanna_Pass_To_The_Method];

你应该很高兴。

于 2016-04-20T19:38:31.530 回答
0

除了其他海报指出的问题外,您的 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", @"");
}
于 2016-04-20T20:00:55.003 回答