1

我正在尝试制作一个简单的应用程序,该应用程序列出了 iPod 库中的所有视频(电影、电视节目等),并允许用户播放他们选择的任何视频。该应用程序正在使用一个故事板,其中包含一个带有 UITableView 的 NavigationController。

看起来查询实际上是在获取 MPMediaItems,但单元格并未使用每部电影的标题进行更新。你能看看这个并告诉我我做错了什么吗?

这是.h:

#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>

@interface MovieListViewController : UITableViewController
{
    MPMediaQuery * _movieQuery;
    NSArray * _movieArray;
}

@property (nonatomic,strong) MPMediaQuery *movieQuery;
@property (nonatomic,strong) NSArray *movieArray;

@end

这是.m:

#import "MovieListViewController.h"

@interface MovieListViewController ()

@end

@implementation MovieListViewController

@synthesize movieQuery = _movieQuery,
            movieArray = _movieArray;

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;

    MPMediaPropertyPredicate *predicate = [MPMediaPropertyPredicate predicateWithValue:[NSNumber numberWithInteger:MPMediaTypeAnyVideo] forProperty:MPMediaItemPropertyMediaType];

    MPMediaQuery *movieQuery = [[MPMediaQuery alloc] init];
    [movieQuery addFilterPredicate:predicate];

    NSLog(@"movieQuery got back %u results",[[movieQuery items]count]);

    self.movieArray = [movieQuery items];

}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Dispose of any resources that can be recreated.
    self.movieQuery = nil;
    self.movieArray = nil;
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

/*- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 0;
}*/

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    NSLog(@"UITableView has %u rows",[self.movieArray count]);
    return [self.movieArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    // Configure the cell...

    MPMediaItem *item = [[self.movieQuery items]objectAtIndex:[indexPath row]];
    cell.textLabel.text = [item valueForProperty:MPMediaItemPropertyTitle];
    return cell;
}
4

1 回答 1

1

问题在于这一行:

MPMediaItem *item = [[self.movieQuery items]objectAtIndex:[indexPath row]];

将其更改为:

MPMediaItem *item = [movieArray objectAtIndex:[indexPath row]];

您没有向movieQuery对象添加任何内容,而是在其中创建了一个本地MPMediaQuery对象viewDidLoad并使用了它。

另一种解决方案:

更改viewDidLoad类似:

- (void)viewDidLoad
{
    [super viewDidLoad];

    MPMediaPropertyPredicate *predicate = [MPMediaPropertyPredicate predicateWithValue:[NSNumber numberWithInteger:MPMediaTypeAnyVideo] forProperty:MPMediaItemPropertyMediaType];

    self.movieQuery = [[MPMediaQuery alloc] init];
    [movieQuery addFilterPredicate:predicate];

    NSLog(@"movieQuery got back %u results",[[self.movieQuery items]count]);

    self.movieArray = [self.movieQuery items];

}
于 2013-08-02T04:20:44.240 回答