Tuesday, March 21, 2023
HomeiOS DevelopmentUICollectionView knowledge supply and delegates programmatically

UICollectionView knowledge supply and delegates programmatically


UICollectionViewCell programmatically

If you would like so as to add views to your cell, it’s best to use the init(body:) methodology, and arrange your view hierarchy there. As a substitute of awakeFromNib it’s best to fashion your views within the init methodology as effectively. You possibly can reset every little thing inside the same old prepareForReuse methodology. As you may see by utilizing anchors typically it is price to ditch IB totally. 🎉

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 traces. You possibly can implement loadView and create your UICollectionView object there. Retailer a weak reference of it contained in the controller, and the remaining 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,
                        structure collectionViewLayout: UICollectionViewLayout,
                        sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: collectionView.bounds.width, top: 44)
    }

    func collectionView(_ collectionView: UICollectionView,
                        structure collectionViewLayout: UICollectionViewLayout,
                        insetForSectionAt part: Int) -> UIEdgeInsets {
        return UIEdgeInsets(high: 0, left: 0, backside: 0, proper: 0) 
    }

    func collectionView(_ collectionView: UICollectionView,
                        structure collectionViewLayout: UICollectionViewLayout,
                        minimumInteritemSpacingForSectionAt part: Int) -> CGFloat {
        return 0
    }

    func collectionView(_ collectionView: UICollectionView,
                        structure 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 person interfaces! 😅

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments