我正在开发一个应用程序,我必须在表格视图中显示视频列表。我想像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;
}
答案 0 :(得分: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)
}
}