swift demo2

2019-07-12  本文已影响0人  高乔人

import UIKit
import AVKit
fileprivate let videoListCellID = "videoListCellID"
class xhwViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    view.backgroundColor = UIColor.white
    self.title = "视频列表"
    self.view.addSubview(self.myTableView)
}


lazy var myTableView: UITableView = {
    let myTableView = UITableView(frame: self.view.bounds)
    
    myTableView.rowHeight = 200
    myTableView.separatorStyle = .none
    myTableView.delegate = self
    myTableView.dataSource = self
    myTableView.register(xhwCell.self, forCellReuseIdentifier: videoListCellID)
    return myTableView
}()

lazy var datas: [VideoModel] = {
    let datas = [
        VideoModel(image: "videoScreenshot01", title: "Introduce 3DS Mario", source: "Youtube - 06:32"),
        VideoModel(image: "videoScreenshot02", title: "Emoji Among Us", source: "Vimeo - 3:34"),
        VideoModel(image: "videoScreenshot03", title: "Seals Documentary", source: "Vine - 00:06"),
        VideoModel(image: "videoScreenshot04", title: "Adventure Time", source: "Youtube - 02:39"),
        VideoModel(image: "videoScreenshot05", title: "Facebook HQ", source: "Facebook - 10:20"),
        VideoModel(image: "videoScreenshot06", title: "Lijiang Lugu Lake", source: "Allen - 20:30")
    ]
    return datas
}()

}
extension xhwViewController:UITableViewDelegate{

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let playerVC = AVPlayerViewController.init()
    let path = Bundle.main.path(forResource: "L", ofType: "mp4")
    playerVC.player = AVPlayer.init(url: URL.init(fileURLWithPath: path!))
    //        self.navigationController?.pushViewController(playerVC, animated: true)
    self.present(playerVC, animated: true) {
        playerVC.player?.play()
    }
    
}

}
extension xhwViewController:UITableViewDataSource{
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: videoListCellID) as! xhwCell
    cell.cellModel = self.datas[indexPath.row]
    return cell
}


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    
    
    return self.datas.count
}

}

import UIKit
struct VideoModel {
var image: String
var title: String
var source: String
}
class xhwCell: UITableViewCell {

//预览图
lazy var preview: UIImageView = {
    let preview = UIImageView.init(frame: CGRect.init(x: 0, y: 0, width: screenW, height: 200))
    return preview
}()

//影片标题
lazy var titleLB: UILabel = {
    let titleLB = UILabel.init(frame: CGRect.init(x: (screenW-200)*0.5, y: 200-50, width: 200, height: 30))
    titleLB.font = .systemFont(ofSize: 15)
    titleLB.textColor = .white
    titleLB.textAlignment = .center
    return titleLB
}()


//播放
lazy var playIcon: UIImageView = {
    let playIcon = UIImageView.init(frame: CGRect.init(x: (screenW-80)*0.5, y: 60, width: 80, height: 80))
    playIcon.image = UIImage(named: "playBtn")
    return playIcon
}()





override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
    
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    
    if !self.isEqual(nil){
    self.selectionStyle = .none
    self.contentView.addSubview(self.preview)
    self.contentView.addSubview(self.titleLB)
    self.contentView.addSubview(self.playIcon)
    }
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}


var cellModel: VideoModel! {
    
    didSet{
        self.titleLB.text = cellModel.title
        self.preview.image = UIImage(named: cellModel.image)
    }
    
}

}

上一篇下一篇

猜你喜欢

热点阅读