自學 iOS,自學ios
做了這個小demo 之後 感覺OC 和swift 還是有很大的差別的 自己還是要去多看些swift的文法 用的不是很熟練
1.這個demo 的資源檔 我都是用原工程的
2.同樣的自訂cell 的時候 用的是“SnapKit”這個庫
3.其實這一個demo的主要就是自訂cell,思想和OC 是一樣的 總感覺swift寫的是那麼的彆扭,可能還是不熟悉文法吧,還是要多看多練
效果
代碼 自訂cell 的代碼
import UIKitimport SnapKitstruct video { let image: String let title: String let source: String }class VideoViewCell: UITableViewCell { public var titleLabel:UILabel? public var picImageView:UIImageView? public var startImageView:UIImageView? override func awakeFromNib() { super.awakeFromNib() } required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) } override init(style: UITableViewCellStyle, reuseIdentifier: String?) { super.init(style: style, reuseIdentifier: reuseIdentifier); self.setUpUI() } func setUpUI() { self.picImageView = UIImageView.init() self.addSubview(self.picImageView!) self.startImageView = UIImageView.init() startImageView?.image = UIImage.init(named: "playBtn") self.addSubview(self.startImageView!) self.titleLabel = UILabel.init() self.titleLabel?.textColor = UIColor.white; self.titleLabel?.textAlignment = .center self.addSubview(self.titleLabel!) self.picImageView?.snp.makeConstraints { (make) in make.top.equalTo(self).offset(0) make.left.equalTo(self).offset(0) make.width.equalTo(SCREEN_WIDTH) make.height.equalTo(self) } self.startImageView?.snp.makeConstraints({ (make) in make.center.equalTo(self.snp.center) make.width.equalTo(100) make.height.equalTo(100) }) self.titleLabel?.snp.makeConstraints({ (make) in make.top.equalTo((self.startImageView?.snp.bottom)!).offset(20) make.width.equalTo(SCREEN_WIDTH) make.height.equalTo(20) }) } override func setSelected(_ selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated) // Configure the view for the selected state }}
控制器的代碼
import UIKitimport AVKitimport AVFoundationlet SCREEN_WIDTH = UIScreen.main.bounds.size.widthlet SCREEN_HEIGHT = UIScreen.main.bounds.size.heightclass ViewController : UIViewController, UITableViewDataSource,UITableViewDelegate { lazy var tableView = UITableView() let array:Array<Any> = [] var data = [ video(image: "videoScreenshot01", title: "Introduce 3DS Mario", source: "Youtube - 06:32"), video(image: "videoScreenshot02", title: "Emoji Among Us", source: "Vimeo - 3:34"), video(image: "videoScreenshot03", title: "Seals Documentary", source: "Vine - 00:06"), video(image: "videoScreenshot04", title: "Adventure Time", source: "Youtube - 02:39"), video(image: "videoScreenshot05", title: "Facebook HQ", source: "Facebook - 10:20"), video(image: "videoScreenshot06", title: "Lijiang Lugu Lake", source: "Allen - 20:30") ] override func viewDidLoad() { super.viewDidLoad() self.tableView = UITableView(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT), style: .plain) self.tableView.dataSource = self self.tableView.delegate = self self.tableView.rowHeight = 220; self.view.addSubview(tableView) } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return data.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let customeIdntifier = "videoCell" var cell = tableView.dequeueReusableCell(withIdentifier: customeIdntifier) as? VideoViewCell if cell == nil { cell = VideoViewCell.init(style: UITableViewCellStyle.default, reuseIdentifier: customeIdntifier) } let video = data[indexPath.row] cell?.titleLabel?.text = video.title cell?.picImageView?.image = UIImage.init(named: video.image) return cell! } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { let playViewContoller = AVPlayerViewController() var playerView = AVPlayer() let path = Bundle.main.path(forResource: "emoji zone", ofType: "mp4") playerView = AVPlayer(url: URL(fileURLWithPath: path!)) playViewContoller.player = playerView self.present(playViewContoller, animated: true) { playViewContoller.player?.play() } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. }}