2015-05-16 54 views
1

我正在開發一個應用程序,我必須在視圖中顯示視頻列表。我想在每行中顯示不同的視頻,就像YouTube顯示的一樣。我只添加了mediaplayerFramework。請告訴我接下來我該做什麼?任何人都可以詳細告訴我。?如何在UITableViewCell中添加視頻

這是我的.h文件

@interface videoViewController :UIViewController<UITableViewDataSource,UITableViewDelegate> 
    { 

    NSMutableArray * arrImages; 

    } 

,這是我的.m文件

@interface videoViewController() 

@end 

@implementation videoViewController 

- (void)viewDidLoad { 
[super viewDidLoad]; 

UITableView *videoTable= [[UITableView alloc]initWithFrame:CGRectMake(0, 0, Screen_width, Screen_height)]; 
videoTable.dataSource=self; 
videoTable.delegate=self; 
arrImages=[[NSMutableArray alloc]initWithObjects:@"video1",@"video2",@"video3",@"video4",@"video5",@"video6",@"video7", nil]; 
[self.view addSubview:videoTable]; 

} 

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ 
return 1; 

} 
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 

return [arrImages count]; 

} 

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 

    static NSString *cellIdentifier [email protected]"CellID"; 
    UITableViewCell *cell = [tableView  dequeueReusableCellWithIdentifier:cellIdentifier]; 
    if (cell==nil) { 
    cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 


} 
    return cell; 
} 
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ 

    return 100; 
} 

回答

1

這是小區追加視頻的一種方式

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 

      static NSString *cellIdentifier [email protected]"CellID"; 
      UITableViewCell *cell = [tableView  dequeueReusableCellWithIdentifier:cellIdentifier]; 
      if (cell==nil) { 
      cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 
       } 


      NSURL *videoURL = [NSURL URLWithString:videoURL]; 
         moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoURL]; 
         [moviePlayer setControlStyle:MPMovieControlStyleNone]; 
         moviePlayer.scalingMode = MPMovieScalingModeAspectFit; 
         [moviePlayer.view setFrame:CGRectMake(10.0, 0.0, 150.0 , 100.0)]; 
         [cell.contentView addSubview:moviePlayer.view]; 
         moviePlayer.view.hidden = NO; 
         [moviePlayer prepareToPlay]; 
         [moviePlayer play]; 
    return cell; 
} 

2.但在細胞中加入視頻的最佳方式是繼承一個UITableViewCell並添加的MPMoviePlayerController作爲其屬性

@implementation CustomVideoCell 
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self) { 
     self.movie = [[MPMoviePlayerController alloc] init]; 
     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 - 80, self.bounds.size.height); 
} 
+0

NSURL * videoURL = [NSURL URLWithString:videoURL]; 是不是? –

+0

mpmovieplayercontroller已被棄用從iOS 9 – joey

+0

@ joey-不要指望有人會爲你找到解決方案嗎?你爲什麼不研究一下,併爲這個問題增加另一個答案,以便它對SO成員有幫助。 – Vizllx