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

    collectionを呼び出すときに使用できるようになりましたgetInfoFormediaItem:

于 2016-04-20T18:57:45.850 に答える
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 が > 0 であるため、値 1 をチェックする前に最初に一致します。したがって、「if ... == 1」は決して true と評価されません。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 に答える