在UITableViewCell中播放video
我试图在单元格中播放video,而不是全屏video显示。 我正在使用MPMoviePlayerController
来达到这个目的。
我已经定义了
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;
在全球范围内定义,它并不仅仅局限于单元格,而是在我向下滚动时继续下降。 我不确定除了我所描述的以外,还有什么其他的方式来实现这一点,显然这似乎不是正确的方法。 如果有人能指引我走向正确的方向,我将不胜感激。
谢谢
千万不要将子视图添加到单元格中:
[cell addSubview:moviePlayer.view];
只将它添加到contentView:
[cell.contentView addSubview:moviePlayer.view];
你所犯的另一个大错误就是你忘记了这个单元可以被重用。 当用户滚动时,该单元格被用于表格的不同行。 所以它被重用,现在电影播放器视图仍然在它,即使这是现在的表错误的行。 您不仅需要将电影播放器视图添加到正确的单元格,还需要将其从所有错误的单元格(在代码的else
部分)中删除 。
每当细胞出现在屏幕上时,你都不应该分配新的对象。 你应该inheritance一个UITableViewCell的子类并添加一个MPMoviePlayerController作为一个属性,它可以被设置为播放或者在没有video的情况下停止和隐藏视图。 所以你的代码应该看起来更像是:
#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
不要为每个单元格创build播放器,而是在调用以下UITableViewDelegate方法时,可以在单元格上创build并覆盖它。 你仍然可以看到一个看起来像玩家的图像,添加到每个单元格中,以便给出每个单元格是玩家的外观和感觉。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath