我正在尝试整理一个简单的应用程序,列出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;
}
答案 0 :(得分: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];
}