我的主要目标是能够单击表格视图项目并加载视频。表格视图填充了文档目录的内容,我已经能够成功地做到这一点并将文件名添加到单元格的标签中,我使用以下代码完成了此操作:
void)viewDidLoad
{
[super viewDidLoad];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSString *dataPath = documentsDirectory;
filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:dataPath error:nil];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [filePathsArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"SimpleTableCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [filePathsArray objectAtIndex:indexPath.row];
return cell;
}
但是我发现困难的部分是打开文件部分。到目前为止,这是我的代码:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"didSelectRowAtIndexPath");
myURL=[NSURL URLWithString:[filePathsArray objectAtIndex:indexPath.row]];
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:myURL];
[self.view addSubview:moviePlayerController.view];
moviePlayerController.fullscreen = YES;
[moviePlayerController play];
[self setController:moviePlayerController];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
当用户单击 UITableView 单元格时会发生什么情况,视频播放器会全屏打开,并且一直说“正在加载...”并且视频没有加载,调试器中也没有报告错误。应该将变量 myURL 设置为等于什么?任何帮助将不胜感激。