【问题标题】:call NSString whit multiple parameters使用多个参数调用字符串
【发布时间】:2016-08-13 15:40:31
【问题描述】:

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

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

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

【问题讨论】:

  • 你在那里进行的执行选择器没有必要而且有点奇怪。它始终为 0 的原因是因为您从不将参数传递给您的方法。所以 [nil count] 总是 0。你应该直接调用它。 [self getInfoForMediaItem: someMPMediaItemCollection];

标签: ios objective-c nsstring mpmediaitemcollection


【解决方案1】:
  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

【讨论】:

  • 您好!感谢您的回复。是的,只是'返回 trackCount;' .关于 :someList 我有使用未声明的标识符 someList,它需要 MPMediaItemCollection 类型的参数。什么参数?感谢您的耐心等待
  • 我使用 someList 是为了作为占位符。您需要将其替换为 MPMediaItemCollection 的实际实例。
  • 你能举个例子吗?在 cellForRowAtIndexPath 中,我需要一个 MPMediaItemCollection 的实例吗? MPMediaItemCollection *count;
  • MPMediaQuery *playlistsQuery = [MPMediaQuery playlistsQuery]; NSArray *playl = [playlistsQuery collections]; MPMediaItem *rowItem = [playl objectAtIndex:indexPath.row]; 但返回 1 首歌曲
  • 用更多细节更新您的问题。
【解决方案2】:

你根本不需要调用这个语句:

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

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

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

你应该很高兴。

【讨论】:

  • 有了 This_Is_A_List_You_Wanna_Pass_To_The_Method 我需要通过什么?它需要一个 MPMediaItemCollection 类型的参数
  • 我尝试MPMediaQuery *playlistsQuery = [MPMediaQuery playlistsQuery]; NSArray *playl = [playlistsQuery collections]; MPMediaItem *rowItem = [playl objectAtIndex:indexPath.row]; MPMediaItemCollection * collection = [[MPMediaItemCollection alloc] initWithItems:[NSArray arrayWithObject:rowItem]];,但总是返回 1。
【解决方案3】:

除了其他发帖者指出的问题之外,你的 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", @"");
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-19
    • 1970-01-01
    • 2013-06-23
    • 1970-01-01
    相关资源
    最近更新 更多