2013-04-02 106 views
5

我试图在单元本身播放视频而不是全屏视频显示。为此,我正在使用MPMoviePlayerController在UITableViewCell中播放视频

我在执行部分cellForRowAtIndexPath:

定义

MPMoviePlayerController *moviePlayer; 

然后我做这个

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;,它并不仅限于它的单元格,而是随着向下滚动而不断下降。我不确定除了我所描述的之外,还有什么其他方式来实现这一点,显然这似乎不是正确的方式。如果有人能指引我走向正确的方向,我将不胜感激。

感谢

回答

8

一个子视图决不添加到单元格:

[cell addSubview:moviePlayer.view]; 

它仅添加到内容查看:

[cell.contentView addSubview:moviePlayer.view]; 

你正在做的另一个大错误是,你”重新忘记这个细胞可以重复使用;当用户滚动时,该单元格将用于表格的不同行。所以它被重用,现在电影播放器​​视图仍然存在,即使这现在是表格的错误行。您不仅需要电影播放器​​视图添加到右侧单元格中,您需要从删除它从所有错误的单元格中(在您的代码的else部分中)。

+0

亚光,我将如何删除它? – Jonathan

+0

用'[cell.contentView removeFromSuperview]'? – Jonathan

+0

我这样做,但是当我向下滚动然后向上滚动时,视频不在单元格中了 – Jonathan

2

而不是为每个单元格创建播放器,而是在调用以下UITableViewDelegate方法时,可以在单元格上创建并覆盖它。您仍然可以看到一个看起来像玩家的图像,并将其添加到每个单元格中,以便为每个单元格的玩家提供外观和感觉。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
8

每当单元格出现在屏幕上时,您都不应该分配新对象。您应该创建一个UITableViewCell的子类并添加一个MPMoviePlayerController作为一个属性,该属性可以设置为播放或停止并隐藏视图(如果没有视频)。所以,你的代码看起来应该更像:

#import CustomCell.h  
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellIdent"]; 
    if (!cell) { 
     cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellIdent"]]; 
    } 

    NSDictionary *dict = dataArray[indexPath.row]; 
    if ([dict[@"type_of_post"] isEqualToString:@"video"]) { 
     NSString *path = @"http://<path to your video>"; 
     [cell.movie setContentURL:[NSURL URLWithString:path]]; 
     [cell.movie prepareToPlay]; 
     [cell.movie play]; 
     cell.movie.view.hidden = NO; 
    } else { 
     [cell.movie stop]; 
     cell.movie.view.hidden = YES; 
     } 

现在的情况下,你不知道一个小区应该是什么样子 CustomCell.h:

@interface CustomCell : UITableViewCell 
@property (nonatomic, retain) MPMoviePlayerController *movie; 
@end 

CustomCell.m:

@implementation CustomCell 
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self) { 
     self.movie = [[MPMoviePlayerController alloc] init]; 
     self.movie.controlStyle = MPMovieControlStyleNone; 
     self.movie.scalingMode = MPMovieScalingModeAspectFit; 
     [self.contentView addSubview:self.movie.view]; 
    } 
    return self; 
} 
- (void)layoutSubviews { 
    [super layoutSubviews]; 
    self.movie.frame = CGRectMake(10, 0, self.bounds.size.width - 20, self.bounds.size.height); 
} 
@end 
+3

在layoutSubview中,它应该是self.movi​​e.view.frame。 – Sebyddd