2015-05-16 6 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

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

Смежные вопросы