【问题标题】:how to add Videos in UITableViewCell如何在 UITableViewCell 中添加视频
【发布时间】:2015-05-16 06:18:38
【问题描述】:

我正在开发一个应用程序,我必须在表格视图中显示视频列表。我想在每一行显示不同的视频,就像 youtube 节目一样。我只添加了 mediaplayerFramework。请告诉我接下来我要做什么?谁能详细告诉我?

这是我的 .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 =@"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;
 }

【问题讨论】:

    标签: ios uitableview video tablerow


    【解决方案1】:

    1. 这是在单元格中添加视频的一种方式

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
              static NSString *cellIdentifier =@"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);
     }
    

    在 Swift 4 中:

            static let tableViewCellIdentifier = "CellID"
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell: UITableViewCell? = tableView.dequeueReusableCell(withIdentifier: tableViewCellIdentifier)
        if cell == nil {
            cell = UITableViewCell(style: .default, reuseIdentifier: tableViewCellIdentifier)
        }
    
    
        let videoURL = URL(string: videoURL ?? "")
        if let videoURL = videoURL {
            moviePlayer = MPMoviePlayerController(contentURL: videoURL)
        }
        moviePlayer.controlStyle = .none
        moviePlayer.scalingMode = .aspectFit
        moviePlayer.view.frame = CGRect(x: 10.0, y: 0.0, width: 150.0, height: 100.0)
        cell?.contentView.addSubview(moviePlayer.view)
        moviePlayer.view.hidden = false
        moviePlayer.prepareToPlay()
        moviePlayer.play()
        return cell!
    }
    

    ......

        class CustomVideoCell {
        init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
            super.init(style: style, reuseIdentifier: reuseIdentifier)
    
            movie = MPMoviePlayerController()
            movie.scalingMode = .aspectFit
            contentView.addSubview(movie.view)
    
        }
    
        func layoutSubviews() {
            super.layoutSubviews()
            movie.frame = CGRect(x: 10, y: 0, width: bounds.size.width - 80, height: bounds.size.height)
        }
    }
    

    【讨论】:

    • NSURL *videoURL = [NSURL URLWithString:videoURL];对吗??
    • mpmovieplayercontroller 自 iOS 9 起已弃用
    • @joey- 不要指望有人会为您找到解决方案?您为什么不研究一下并为这个问题添加另一个答案,以便对 SO 成员有所帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-09
    • 1970-01-01
    • 1970-01-01
    • 2011-11-10
    • 1970-01-01
    • 2021-06-05
    相关资源
    最近更新 更多