Skip to content

Instantly share code, notes, and snippets.

@muhlenXi
Last active December 2, 2018 03:23
Show Gist options
  • Save muhlenXi/60a9be0eaddf0636869de6929ee593d4 to your computer and use it in GitHub Desktop.
Save muhlenXi/60a9be0eaddf0636869de6929ee593d4 to your computer and use it in GitHub Desktop.
UICollectionView 的应用 demo
import UIKit
fileprivate let CellIdentifier = "CellIdentifier"
// MARK: InterCell
class InterCell: UICollectionViewCell {
lazy var thumbnail: UIImageView = {
let imageView = UIImageView()
imageView.backgroundColor = UIColor.white
return imageView
}()
lazy var nameLabel: UILabel = {
let label = UILabel()
label.font = UIFont.PingFangSC(size: 14)
label.text = "title"
label.textAlignment = .center
label.textColor = UIColor(red: 48, green: 48, blue: 48, alpha: 1.0)
return label
}()
override init(frame: CGRect) {
super.init(frame: frame)
self.setupSubViews()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func setupSubViews() {
self.contentView.backgroundColor = UIColor(red: 247, green: 247, blue: 247, alpha: 247)
self.contentView.addSubview(thumbnail)
self.contentView.addSubview(nameLabel)
nameLabel.snp.makeConstraints { (maker) in
maker.leading.equalTo(8)
maker.trailing.equalTo(-8)
maker.height.equalTo(15)
maker.bottom.equalTo(-8)
}
thumbnail.snp.makeConstraints { (maker) in
maker.leading.equalTo(14)
maker.trailing.equalTo(-14)
maker.top.equalTo(14)
maker.height.equalTo(thumbnail.snp.width)
}
}
}
// MARK: InterestedViewController
class InterestedViewController: UIViewController {
let itemWidth = floor((UIScreen.main.bounds.size.width - CGFloat(25))/CGFloat(4.0))
lazy var collectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .vertical
let collection = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)
collection.bounces = true
collection.backgroundColor = UIColor.white
collection.delegate = self
collection.dataSource = self
return collection
}()
// MARK: Life cycle
override func viewDidLoad() {
super.viewDidLoad()
self.setupSubViews()
}
}
//MARK: - Assistant
extension InterestedViewController {
func setupSubViews() {
self.view.backgroundColor = UIColor.white
self.navigationItem.title = "CollectionView 演示"
self.view.addSubview(collectionView)
collectionView.register(InterCell.self, forCellWithReuseIdentifier: CellIdentifier)
collectionView.snp.makeConstraints { (maker) in
maker.leading.trailing.equalTo(0)
maker.top.equalTo(8)
maker.bottom.equalTo(-kSafeAreaBottomInset)
}
}
}
// MARK: UICollectionViewDataSource
extension InterestedViewController: UICollectionViewDataSource {
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 36
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CellIdentifier, for: indexPath) as! InterCell
return cell
}
}
// MARK: UICollectionViewDelegate
extension InterestedViewController: UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
collectionView.deselectItem(at: indexPath, animated: true)
}
}
// MARK: UICollectionViewDelegateFlowLayout
extension InterestedViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: itemWidth, height: itemWidth+CGFloat(20))
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: 8, left: 5, bottom: 8, right: 5)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 5
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment