UITableViewCell没有在iOS 7中的单元格上显示第一个MPMoviePlayerController视频?

时间:2014-05-26 22:04:49

标签: ios iphone ipad uitableview

我正在尝试在视频添加完美的UITableViewCell上添加视频,但问题是当我添加第二个视频时,UITableViewCell上的第一个单元格显示为黑色,第二个单元格显示视频。

我希望我的所有单元格显示视频第一帧(视频图片)

但它只显示最后一个(最后一个单元格)

然后我添加UIButton来播放完美运行的每个单元格视频。

我的问题是我希望我的所有小区在每个UITableViewCell上显示视频第一帧(视频图片)。

但问题是如果只有一个单元格添加,那么它会完美地显示视频第一张图片。然后我点击按钮,它开始完美的视频,但是当我添加第二个视频时,第一个视频得到黑色,第二个视频完美地工作。

这是我的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section==0) {


   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"listData"];

   if (cell == nil) {

        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil] autorelease];
      cell.selectedBackgroundView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@".png"] ]; 
        cell.backgroundView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@".png"] ];

         [cell setBackgroundColor:[UIColor clearColor]];


       UILabel * lbl =[[UILabel alloc]initWithFrame:CGRectMake(330, 80, 135, 41)];
       lbl.backgroundColor = [UIColor clearColor];
       lbl.text = @"Play Video Here";
      [cell.contentView addSubview:lbl];





       NSInteger x = [[NSUserDefaults standardUserDefaults]  integerForKey:@"cellRowNumber"];
       NSLog(@"x is %d",x);

       if (x<1) {

           x = 0;
       }


       NSURL* url = [UIAppDelegate.videoUrlsArray objectAtIndex:indexPath.row];

       NSLog(@"selected tableview row is %d",indexPath.row);

      NSInteger p = indexPath.row ;
       NSLog(@"P is %d",p);




       secView2 =[[UIView alloc]initWithFrame:CGRectMake(84,5,600,170)];
       secView2.tag = indexPath.row;
       secView2.backgroundColor = [UIColor redColor];
       [cell.contentView addSubview:secView2];



       moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
       moviePlayer.view.frame = CGRectMake(0,0,600,170);
       moviePlayer.controlStyle = MPMovieControlStyleEmbedded;
       [moviePlayer prepareToPlay];
       moviePlayer.shouldAutoplay = NO;
       [secView2 addSubview:moviePlayer.view];


       playBtn = [UIButton buttonWithType:UIButtonTypeCustom];
       playBtn.tag = indexPath.row;
       playBtn.frame = CGRectMake(0,0,600,170);
       [playBtn setBackgroundColor:[UIColor clearColor]];
       playBtn.tag = indexPath.row;
       [playBtn addTarget:self action:@selector(didButtonTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
       [secView2 addSubview:playBtn];



   }


   // moviePlayer.contentURL = [UIAppDelegate.videoUrlsArray objectAtIndex:indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    //[self.moviePlayer pause];
    return cell;

    }

else {

// Nothing

       }

//cell.textLabel.text = [UIAppDelegate.youTubeUrlsArray objectAtIndex:indexPath.row];
 cell.selectionStyle = UITableViewCellSelectionStyleNone;


 return cell;

    }
 }


-(void)didButtonTouchUpInside:(id)sender{


UIButton *btn = (UIButton *) sender;
CGRect buttonFrameInTableView = [btn convertRect:btn.bounds toView:table];
NSIndexPath *indexPath = [table indexPathForRowAtPoint:buttonFrameInTableView.origin];

NSLog(@"%d",indexPath.row);



secView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, 768, 960)];
secView.backgroundColor = [UIColor redColor];
[self.view addSubview:secView];


NSURL* url = [UIAppDelegate.videoUrlsArray objectAtIndex:indexPath.row];

// Now set up the movie player controller and play the movie.
player = [[MPMoviePlayerController alloc] initWithContentURL: url];
[[player view] setFrame: CGRectMake(0, 50, 768, 910)];  // frame must match parent view
[secView addSubview: [player view]];
[player play];



//btn_hideSecview = [UIButton buttonWithType:UIButtonTypeCustom];
btn_hideSecview =[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 70, 45)];
btn_hideSecview.backgroundColor = [UIColor greenColor];
// btn_hideSecview.frame = CGRectMake(600, 0, 70, 45);
[btn_hideSecview setBackgroundColor:[UIColor whiteColor]];
[btn_hideSecview addTarget:self action:@selector(method_Hide_Secview) forControlEvents:UIControlEventTouchUpInside];
[secView addSubview:btn_hideSecview];
}

非常欢迎任何想法或建议。

1 个答案:

答案 0 :(得分:0)

据我所知,你创造了&#34; secView2&#34;和#34; moviePlayer&#34;作为本地属性,这意味着您只使用这些类的一个实例。对于表委托方法cellForRowAtIndexPath的每次调用 - 创建MoviePlayer和UIView的新对象。如果您需要在该委托之外进行访问,请准备一些将存储该实例的DataSource NSMutableArray。

MPMoviePlayerController * newPlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];

UIView * secView2 = [[UIView alloc] initWithFrame:CGRectMake(84,5,600,170)];