在UITableViewCell中播放视频 [英] playing videos in UITableViewCell
问题描述
我正在尝试在单元格中播放视频而不是全屏视频显示。我为此目的使用 MPMoviePlayerController
。
I am trying to play videos in the cells itself instead of a fullscreen video display. I am using MPMoviePlayerController
for this purpose.
我已定义
MPMoviePlayerController *moviePlayer;
执行部分
然后进入 cellForRowAtIndexPath:
我这样做
cell = [tableView dequeueReusableCellWithIdentifier:simpleTableVideoIdentifier];
if (cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableVideoIdentifier];
}
NSDictionary *dictionary = [dataArray objectAtIndex:indexPath.section];
NSDictionary *data = dictionary;
if ([data[@"type_of_post"] isEqualToString:@"video"]) {
NSString *path = data[@"video"];
NSURL *videoURL = [NSURL URLWithString:path];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
[moviePlayer setControlStyle:MPMovieControlStyleNone];
moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
[moviePlayer.view setFrame:CGRectMake(10.0, 0.0, 300.0 , 400.0)];
[cell addSubview:moviePlayer.view];
moviePlayer.view.hidden = NO;
[moviePlayer prepareToPlay];
[moviePlayer play];
}else{
//do something else
}
显然我在 heightForRowAtIndexPath中指定了一个高度:
但它指向 MPMoviePlayerController * moviePlayer;
是全局定义的,它不仅仅坚持其单元格,而是在向下滚动时继续向下。我不确定除了我所描述的之外还有什么其他方式来实现这一点,这显然似乎不是正确的方法。如果有人能引导我走向正确的方向,我将非常感激。
but the point it as MPMoviePlayerController *moviePlayer;
is defined globally it doesn't stick to just its cell but keeps on coming down as I scroll down. I am not sure what other way to implement this other than what I described, which clearly doesn't seem the right way to go about it. I would really appreciate if someone can guide me to the right direction.
谢谢
推荐答案
永远不要在单元格中添加子视图:
Never add a subview to the cell:
[cell addSubview:moviePlayer.view];
仅将其添加到contentView:
Add it only to the contentView:
[cell.contentView addSubview:moviePlayer.view];
你所犯的另一个重大错误就是忘记了这个细胞可以重复使用;当用户滚动时,该单元格用于表格的不同行。因此它被重用,现在电影播放器视图仍在其中,即使现在这是表的错误行。您不仅需要将添加电影播放器视图添加到正确的单元格,还需要从所有错误的单元格中删除(在 else <中/ code>部分代码。)
The other big mistake you're making is that you're forgetting that this cell can be reused; as the user scrolls, the cell be used for a different row of the table. So it gets reused, and now the movie player view is still in it even though this is now the wrong row of the table. You need not only to add the movie player view to the right cell, you need to remove it from all the wrong cells (in the else
part of your code).
这篇关于在UITableViewCell中播放视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!