Sunday, June 14, 2026
HomeiOS DevelopmentUICollectionView information supply and delegates programmatically

UICollectionView information supply and delegates programmatically

[ad_1]

UICollectionViewCell programmatically

If you would like so as to add views to your cell, you must use the init(body:) technique, and arrange your view hierarchy there. As an alternative of awakeFromNib you must model your views within the init technique as effectively. You may reset the whole lot inside the same old prepareForReuse technique. As you may see by utilizing anchors generally it is value to ditch IB fully. 🎉

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 extra traces. You may 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 information: [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.information.depend
    }

    func collectionView(_ collectionView: UICollectionView,
                        cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: Cell.identifier, for: indexPath) as! Cell
        let information = self.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 {
        return CGSize(width: collectionView.bounds.width, peak: 44)
    }

    func collectionView(_ collectionView: UICollectionView,
                        format collectionViewLayout: UICollectionViewLayout,
                        insetForSectionAt part: Int) -> UIEdgeInsets {
        return UIEdgeInsets(high: 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 simple. Anchors are actually highly effective, Interface Builder is extraordinarily useful, however generally it is simply sooner to create your views from code. The selection is yours, however please do not be afraid of coding person interfaces! 😅

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments