类方法关键字static
static func createInterests() -> [CollectionModel] {
return [
CollectionModel(title: "Hello there, i miss u.", descriptions: "We love backpack and adventures! We walked to Antartica yesterday, and camped with some cute pinguines, and talked about this wonderful app idea. 🐧⛺️✨", featuredImage: UIImage(named: "hello")!),
CollectionModel(title: "🐳🐳🐳🐳🐳", descriptions: "We love romantic stories. We walked to Antartica yesterday, and camped with some cute pinguines, and talked about this wonderful app idea. 🐧⛺️✨", featuredImage: UIImage(named: "dudu")!),
]
}
ClassName.self
collectionView .register(CollectionCell.self, forCellWithReuseIdentifier: reuseIdentifier)
didSet
顾名思义, set方法调用之后调用
var data: CollectionModel? {
/*属性观察器
willSet 在新的值被设置之前调用
didSet 在新的值被设置之后立即调用
*/
didSet {
updateUI()
}
}
重写Model的init方法
init(title: String, descriptions: String, featuredImage: UIImage) {
self.title = title
self.descriptions = descriptions
self.featuredImage = featuredImage
}
调用
CollectionModel(title: "Hello there, i miss u.", descriptions: "We love backpack and adventures! We walked to Antartica yesterday, and camped with some cute pinguines, and talked about this wonderful app idea. 🐧⛺️✨", featuredImage: UIImage(named: "hello")!),
extension协议
/*扩展ViewController支持协议*/
extension ViewController: UICollectionViewDataSource, UICollectionViewDelegate {
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return data.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! CollectionCell
cell.data = self.data[indexPath.row]
return cell
}
}
<li><h1>学习代码:<h1></li>
CollectionModel.swift
import UIKit
class CollectionModel: NSObject {
var title: String?
var descriptions: String?
var featuredImage: UIImage?
init(title: String, descriptions: String, featuredImage: UIImage) {
self.title = title
self.descriptions = descriptions
self.featuredImage = featuredImage
}
static func createInterests() -> [CollectionModel] {
return [
CollectionModel(title: "Hello there, i miss u.", descriptions: "We love backpack and adventures! We walked to Antartica yesterday, and camped with some cute pinguines, and talked about this wonderful app idea. 🐧⛺️✨", featuredImage: UIImage(named: "hello")!),
CollectionModel(title: "🐳🐳🐳🐳🐳", descriptions: "We love romantic stories. We walked to Antartica yesterday, and camped with some cute pinguines, and talked about this wonderful app idea. 🐧⛺️✨", featuredImage: UIImage(named: "dudu")!),
CollectionModel(title: "Training like this, #bodyline", descriptions: "Create beautiful apps. We walked to Antartica yesterday, and camped with some cute pinguines, and talked about this wonderful app idea. 🐧⛺️✨", featuredImage: UIImage(named: "bodyline")!),
CollectionModel(title: "I'm hungry, indeed.", descriptions: "Cars and aircrafts and boats and sky. We walked to Antartica yesterday, and camped with some cute pinguines, and talked about this wonderful app idea. 🐧⛺️✨", featuredImage: UIImage(named: "wave")!),
CollectionModel(title: "Dark Varder, #emoji", descriptions: "Meet life with full presence. We walked to Antartica yesterday, and camped with some cute pinguines, and talked about this wonderful app idea. 🐧⛺️✨", featuredImage: UIImage(named: "darkvarder")!),
CollectionModel(title: "I have no idea, bitch", descriptions: "Get up to date with breaking-news. We walked to Antartica yesterday, and camped with some cute pinguines, and talked about this wonderful app idea. 🐧⛺️✨", featuredImage: UIImage(named: "hhhhh")!),
]
}
}
CollectionCell.swift
import UIKit
class CollectionCell: UICollectionViewCell {
let featureImageView = UIImageView(frame: CGRect(x: 0.0, y: 0.0, width: ItemWidth, height: ItemHeight))
let interestTitleLabel = UILabel(frame: CGRect(x: 0.0, y: ItemHeight-50, width: ItemWidth, height: 20))
let interestDetailLabel = UILabel(frame: CGRect(x: 0.0, y: ItemHeight-30, width: ItemWidth, height: 30))
var data: CollectionModel? {
/*属性观察器
willSet 在新的值被设置之前调用
didSet 在新的值被设置之后立即调用
*/
didSet {
updateUI()
}
}
override init(frame: CGRect) {
super.init(frame: frame)
interestTitleLabel.backgroundColor = .gray
interestTitleLabel.textColor = .white
interestTitleLabel.textAlignment = .center
interestTitleLabel.font = UIFont.systemFont(ofSize: 16, weight: 4)
interestDetailLabel.backgroundColor = .white
interestDetailLabel.backgroundColor = .gray
interestDetailLabel.textColor = .white
interestDetailLabel.textAlignment = .center
interestDetailLabel.numberOfLines = 0
interestDetailLabel.font = UIFont.systemFont(ofSize: 10)
contentView.addSubview(featureImageView)
contentView.addSubview(interestTitleLabel)
contentView.addSubview(interestDetailLabel)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private func updateUI() {
featureImageView.image = data?.featuredImage
interestTitleLabel.text = data?.title
interestDetailLabel.text = data?.descriptions
}
override func layoutSubviews() {
super.layoutSubviews()
self.layer.cornerRadius = 5.0
featureImageView.clipsToBounds = true
}
}
ViewController.swift
import UIKit
let YHRect = UIScreen.main.bounds
let YHHeight = YHRect.size.height
let YHWidth = YHRect.size.width
let ItemWidth = YHWidth-40.0
let ItemHeight = YHHeight/3.0
class ViewController: UIViewController {
let backgroundImageView = UIImageView(frame: YHRect)
var collectionView: UICollectionView!
let data = CollectionModel.createInterests()
let reuseIdentifier = "CollectionCell"
let visualEffectView = UIVisualEffectView(effect: UIBlurEffect(style: .light))
override func viewDidLoad() {
super.viewDidLoad()
setupView()
}
func setupView() {
backgroundImageView.image = UIImage(named: "blue")
let collectionLayout = UICollectionViewFlowLayout()
collectionLayout.scrollDirection = .horizontal//滚动方向
collectionLayout.itemSize = CGSize(width: ItemWidth, height: ItemHeight)//cell大小
collectionLayout.minimumLineSpacing = 20//上下间隔
collectionLayout.minimumInteritemSpacing = 20//左右间隔
collectionLayout.sectionInset = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 20)//section边界
collectionView = UICollectionView(frame: CGRect(x: 0.0, y: (YHHeight-ItemHeight)/2, width: YHWidth, height: ItemHeight), collectionViewLayout: collectionLayout)
collectionView.backgroundColor = .clear
collectionView.dataSource = self
collectionView.delegate = self
collectionView .register(CollectionCell.self, forCellWithReuseIdentifier: reuseIdentifier)
visualEffectView.frame = YHRect
view.addSubview(backgroundImageView)
view.addSubview(visualEffectView)
view.addSubview(collectionView)
}
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
/*扩展ViewController支持协议*/
extension ViewController: UICollectionViewDataSource, UICollectionViewDelegate {
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return data.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! CollectionCell
cell.data = self.data[indexPath.row]
return cell
}
}