3.

快速实例化struct

struct VideoModel {
    let image: String
    let title: String
    let source: String
}

实例化:

VideoModel(image: "videoScreenshot01", title: "Introduce 3DS Mario", source: "Youtube - 06:32")

类型转换

类型转换:

as?:返回一个向下转型的类型的可选值
as!:强制转型,并且解包
is:检查能够向下转化成指定类型
as:向上转换成超类
当不确定是否可以转成功时,用as?,确定时,用as!

let cell = tableView.dequeueReusableCell(withIdentifier: reuseIdentifer, for: indexPath) as! VideoCell

其中:!强制解析值,解析前一定确保可选是有值的

函数访问关键字

Public:所有都可以访问
Internal:自己framework访问(默认)
Private:私有的
总的来说,默认就行,不公开就private,写框架的话,就需要public公开接口

重写init

重写类的时候经常提示要添加代码:

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

http://www.jianshu.com/p/66ecd88b4210

<li><h1>学习代码:<h1></li>

VideoCell.swift

import UIKit

struct VideoModel {
    let image: String
    let title: String
    let source: String
}

class VideoCell: UITableViewCell {
    
    let videoImage = UIImageView(frame: CGRect(x: 0, y: 0, width: YHScreenWidth, height: YHScreenHeight/3))
    let videoTitle = UILabel(frame: CGRect(x: 0, y: YHScreenHeight/3-50, width: YHScreenWidth, height: 30))
    let videoSource = UILabel(frame: CGRect(x: 0, y: YHScreenHeight/3-20, width: YHScreenWidth, height: 20))
    /*
     Public:所有都可以访问
     Internal:自己framework访问(默认)
     Private:私有的
     总的来说,默认就行,不公开就private,写框架的话,就需要public公开接口
     */
    private let videoPlay = UIImageView(frame: CGRect(x: 0, y: 0, width: YHScreenWidth, height: YHScreenHeight/3))
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupView()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupView() {
        videoImage.contentMode = .scaleAspectFill
        videoPlay.contentMode = .center
        videoPlay.image = UIImage(named: "playBtn")
        videoTitle.textColor = .white
        videoTitle.font = UIFont(name: "Zapfino", size: 24)
        videoTitle.textAlignment = .center
        videoSource.textColor = .gray
        videoSource.font = UIFont(name: "Avenir Next", size: 14)
        videoSource.textAlignment = .center
        contentView.addSubview(videoImage)
        contentView.addSubview(videoPlay)
        contentView.addSubview(videoTitle)
        contentView.addSubview(videoSource)
    }
    
    func setModel(model: VideoModel) {
        videoImage.image = UIImage(named: model.image)
        videoTitle.text = model.title
        videoSource.text = model.source
    }

    override func awakeFromNib() {
        super.awakeFromNib()
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

ViewController.swift:

import UIKit
import AVKit
import AVFoundation

let YHScreenRect : CGRect = UIScreen.main.bounds
let YHScreenWidth : CGFloat = UIScreen.main.bounds.width
let YHScreenHeight : CGFloat = UIScreen.main.bounds.height

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    
    let tableView = UITableView(frame: YHScreenRect, style: .plain)
    var playViewController: AVPlayerViewController?
    var playView: AVPlayer?
    let reuseIdentifer = "VideoCell"
    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"),
    ]

    override func viewDidLoad() {
        super.viewDidLoad()
        setupView()
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        playView = nil
        playViewController = nil
    }
    //隐藏状态栏
    override var prefersStatusBarHidden: Bool {
        return true
    }
    
    func setupView() {
        tableView.delegate = self
        tableView.dataSource = self
        tableView.register(VideoCell.self, forCellReuseIdentifier: reuseIdentifer)
        view.addSubview(tableView)
    }
    
    func playVideo() {
        /*
         错误处理:
         1.throwing,把函数抛出的错误传递给调用此函数的代码
         2.do-catch
         3.将错误作为可选类型处理
         4.断言此错误根本不会发生
         */
        let path = Bundle.main.path(forResource: "emoji zone", ofType: "mp4")
        /*
         guard与if很相似,区别:
           1.guard必须强制有else语句
           2.只有在guard审查条件成立,guard之后代码才会运行
         */
        if path == nil {
            print("没有该文件!")
            return
        }
        //由于返回了可选类型,并且通过上方判断,可以确定是有值的,然后强制解包
        playView = AVPlayer(url: URL(fileURLWithPath: path!))
        playViewController = AVPlayerViewController()
        playViewController?.player = playView
        self.present(playViewController!, animated: true) {
            self.playView?.play()
        }
    }
    
//MARK: - delegate
    func numberOfSections(in tableView: UITableView) -> Int {
        return 2;
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return datas.count
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return YHScreenHeight/3
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        /*
         !强制解析值,解析前一定确保可选是有值的
         
         类型转换:
            as?:返回一个向下转型的类型的可选值
            as!:强制转型,并且解包
            is:检查能够向下转化成指定类型
            as:向上转换成超类
         当不确定是否可以转成功时,用as?,确定时,用as!
        */
        let cell = tableView.dequeueReusableCell(withIdentifier: reuseIdentifer, for: indexPath) as! VideoCell
        cell.setModel(model: datas[indexPath.row])
        return cell
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        playVideo()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • importUIKit classViewController:UITabBarController{ enumD...
    明哥_Young阅读 9,458评论 1 10
  • 前言 Swift,苹果于2014年WWDC(苹果开发者大会)发布的新开发语言,可与Objective-C共同运行于...
    践行者_Leng阅读 4,162评论 2 1
  • 这是我对 Swift 3.0 整理的笔记,主要内容来自于官方文档,添加了一些指针的内容在最后。该笔记由于只是我个人...
    黄穆斌阅读 7,821评论 19 59
  • 2016年10月12日 Objective-C id为Swift Any Swift 3接口与Objective-...
    魔灵FH阅读 7,900评论 0 19
  • Swift属性 Swift属性将值跟特定的类,结构体,枚举关联。分为存储属性和计算属性,通常用于特定类型的实例。属...
    小小厨师阅读 4,375评论 0 0