ios - 使用多个参数调用 NSString

标签 ios objective-c nsstring mpmediaitemcollection

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

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

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

最佳答案

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

    cell.detailTextLabel.text = [self getInfoFormediaItem:collection];
    
  2. 为什么要将 nil 传递给 withObject: 参数?这就是您的代码转到 else 的原因。 listnil,因此 [list count] 将始终为 0。您需要传递 MPMediaItemCollection 的实际实例。

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

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

关于ios - 使用多个参数调用 NSString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36752632/

相关文章:

iphone - UITableViewCell 附件 Checkmarx 问题,许多单元格仅通过检查一行来检查

iOS - 隐藏在键盘下的 UIAlertController

ios - 如何知道 NSString* 类型的变量是否为空?

iphone - iOS:导航栏项目消失并重新出现

iphone - NSString 到 NSNumber 的格式

cocoa - NSUTF8StringEncoding 导致文件在 TextEdit/Pages 中渲染不正确,但在 BBEdit/vi 中则不会

iphone - 缩放和标记

iphone - 检查应用程序是否从非事件状态恢复到第一次启动?

ios - 在 View Controller 之间传输变量而不会出错

ios - TableViewController 不更新单元格