我正试图在我的UITableViewCell中为我的单元格添加一部电影。
电影似乎播放声音,但没有视觉效果。我想我无法将moviePlayer视图添加到我的手机中。
看看我的代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"FeedCustomCell";
FeedCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
cell.username.text = [NSString stringWithFormat:@"%@", _usernameString];
cell.createdDate.text = [NSString stringWithFormat:@"%@", _createdString];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// First, get the URL for the video file you want to play. For example, if you have an array of the movie file URLs, you'd do this:
FeedCustomCell *cell = [[FeedCustomCell alloc] init];
NSURL *url = [NSURL URLWithString:_videoPathString];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
moviePlayer.controlStyle = MPMovieControlStyleNone;
CGRect previewFrame = CGRectMake(cell.videoView.frame.origin.x, cell.videoView.frame.origin.y, cell.videoView.frame.size.width, cell.videoView.frame.size.height);
moviePlayer.view.frame = previewFrame;
moviePlayer.repeatMode = MPMovieRepeatModeOne;
[cell.videoView addSubview:moviePlayer.view];
[cell.videoView bringSubviewToFront:moviePlayer.view];
[moviePlayer play];
}
有什么想法吗?
答案 0 :(得分:1)
您正在使用此行分配新单元格:
FeedCustomCell *cell = [[FeedCustomCell alloc] init];
但是此单元格在视图层次结构的任何位置都不存在,因此不会显示。如果你试图将电影播放器插入到被点击的现有单元格中,那么你会得到这样的单元格:
FeedCustomCell *cell = (FeedCustomCell *)[tableView cellForRowAtIndexPath:indexPath];
否则,如果您尝试插入新单元格,则需要执行以下操作:
[tableView insertRowsAtIndexPaths:withRowAnimation:]
插入新行。cellForRowAtIndexPath
。答案 1 :(得分:1)
您实际上并未在tableview单元格中创建 moviePlayer 。在didSelectRowAtIndexPath
方法内,您可以创建另一个 FeedCustomCell 并在其中添加 moviePlayer 。您甚至没有在表格视图单元格中添加本地 FeedCustomCell 。
更容易解决问题的方法是替换
FeedCustomCell *cell = [[FeedCustomCell alloc] init];
与
FeedCustomCell *cell = (FeedCustomCell *)[tableView cellForRowAtIndexPath:indexPath];
答案 2 :(得分:0)
当你这样做时
FeedCustomCell *cell = [[FeedCustomCell alloc] init];
在didSelectRowAtIndexPath
中,您正在创建FeedCustomCell
的全新实例 - 您要做的是获取用户点击的单元格。将该行替换为:
FeedCustomCell *cell = (FeedCustomCell *)[tableView cellForRowAtIndexPath:indexPath];