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