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:) methodology, and arrange your view hierarchy there. As an alternative of awakeFromNib you must fashion your views within the init methodology as properly. You possibly can reset all the pieces inside the standard 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
        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 applied")
    }

    override func prepareForReuse() {
        tremendous.prepareForReuse()
        self.reset()
    }

    func reset() {
        self.textLabel.textAlignment = .middle
    }
}

UICollectionView programmatically

Creating assortment view controllers utilizing solely Swift code requires only some extra strains. You possibly can 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
        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.rely
    }

    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,
                        structure collectionViewLayout: UICollectionViewLayout,
                        sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: collectionView.bounds.width, peak: 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 generally it is simply sooner to create your views from code. The selection is yours, however please do not be afraid of coding consumer interfaces! 😅

[ad_2]

Leave a Reply