2013-04-02 12 views
5

私はフルスクリーンビデオディスプレイの代わりにセル自体でビデオを再生しようとしています。私はこの目的のためにMPMoviePlayerControllerを使用しています。UITableViewCellでビデオを再生する

私はその後cellForRowAtIndexPath:

に私はそれheightForRowAtIndexPath:

で高割り当てるこの

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 
      } 

と明らかに行う実装セクション

MPMoviePlayerController *moviePlayer; 

を定義しています3210

しかし、それはMPMoviePlayerController *moviePlayer;として定義されていますが、それはそのセルだけに固執せず、下にスクロールするにつれて下がり続けます。私が記述したもの以外にこれを実装するための他の方法は何もわかりませんが、それは明らかに正しい方法ではないようです。誰かが私を正しい方向に導くことができたら本当に感謝します。

おかげ

答えて

8

は、セルにサブビューを追加することはありません:

[cell addSubview:moviePlayer.view]; 

はcontentViewに追加します:あなたが作っている

[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をプロパティとして追加して、ビデオがない場合は再生または停止して表示を非表示にすることができます。今の場合には、あなたが、細胞が CustomCell.hをどのように見えるべきか分からない

#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; 
     } 

@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

関連する問題