UICollectionViewCell programmatically
If you would like so as to add views to your cell, it is best to use the init(body:)
methodology, and arrange your view hierarchy there. As an alternative of awakeFromNib
it is best to fashion your views within the init methodology as properly. You’ll be able to reset every thing inside the standard prepareForReuse
methodology. As you may see by utilizing anchors typically it is price to ditch IB solely. 🎉
class Cell: UICollectionViewCell {
static var identifier: String = "Cell"
weak var textLabel: UILabel!
override init(body: CGRect) {
tremendous.init(body: body)
let textLabel = UILabel(body: .zero)
textLabel.translatesAutoresizingMaskIntoConstraints = false
self.contentView.addSubview(textLabel)
NSLayoutConstraint.activate([
self.contentView.centerXAnchor.constraint(equalTo: textLabel.centerXAnchor),
self.contentView.centerYAnchor.constraint(equalTo: textLabel.centerYAnchor),
])
self.textLabel = textLabel
self.reset()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been carried out")
}
override func prepareForReuse() {
tremendous.prepareForReuse()
self.reset()
}
func reset() {
self.textLabel.textAlignment = .middle
}
}
UICollectionView programmatically
Creating assortment view controllers utilizing solely Swift code requires just a few further strains. You’ll be able to implement loadView
and create your UICollectionView
object there. Retailer a weak reference of it contained in the controller, and the remainder is identical.
class ViewController: UIViewController {
weak var collectionView: UICollectionView!
var knowledge: [Int] = Array(0..<10)
override func loadView() {
tremendous.loadView()
let collectionView = UICollectionView(body: .zero, collectionViewLayout: UICollectionViewFlowLayout())
collectionView.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(collectionView)
NSLayoutConstraint.activate([
self.view.topAnchor.constraint(equalTo: collectionView.topAnchor),
self.view.bottomAnchor.constraint(equalTo: collectionView.bottomAnchor),
self.view.leadingAnchor.constraint(equalTo: collectionView.leadingAnchor),
self.view.trailingAnchor.constraint(equalTo: collectionView.trailingAnchor),
])
self.collectionView = collectionView
}
override func viewDidLoad() {
tremendous.viewDidLoad()
self.collectionView.dataSource = self
self.collectionView.delegate = self
self.collectionView.register(Cell.self, forCellWithReuseIdentifier: Cell.identifier)
self.collectionView.alwaysBounceVertical = true
self.collectionView.backgroundColor = .white
}
}
extension ViewController: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView,
numberOfItemsInSection part: Int) -> Int {
return self.knowledge.depend
}
func collectionView(_ collectionView: UICollectionView,
cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: Cell.identifier, for: indexPath) as! Cell
let knowledge = self.knowledge[indexPath.item]
cell.textLabel.textual content = String(knowledge)
return cell
}
}
extension ViewController: UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
}
}
extension ViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView,
format collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.bounds.width, peak: 44)
}
func collectionView(_ collectionView: UICollectionView,
format collectionViewLayout: UICollectionViewLayout,
insetForSectionAt part: Int) -> UIEdgeInsets {
return UIEdgeInsets(prime: 0, left: 0, backside: 0, proper: 0)
}
func collectionView(_ collectionView: UICollectionView,
format collectionViewLayout: UICollectionViewLayout,
minimumInteritemSpacingForSectionAt part: Int) -> CGFloat {
return 0
}
func collectionView(_ collectionView: UICollectionView,
format collectionViewLayout: UICollectionViewLayout,
minimumLineSpacingForSectionAt part: Int) -> CGFloat {
return 0
}
}
That was straightforward. Anchors are actually highly effective, Interface Builder is extraordinarily useful, however typically it is simply quicker to create your views from code. The selection is yours, however please do not be afraid of coding consumer interfaces! 😅