HomeiOS DevelopmentUICollectionView knowledge supply and delegates programmatically

UICollectionView knowledge supply and delegates programmatically



· 1 min learn


On this fast UIKit tutorial I will present you tips on how to create a easy UICollectionView with out Interface Builder, however solely utilizing Swift.

UICollectionViewCell programmatically

Should you’d like so as to add views to your cell, you need to use the init(body:) technique, and arrange your view hierarchy there. As an alternative of awakeFromNib you need to fashion your views within the init technique as nicely. You possibly can reset every thing inside the same old prepareForReuse technique. As you may see by utilizing anchors typically it’s price 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
        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 = .heart
    }
}

UICollectionView programmatically

Creating assortment view controllers utilizing solely Swift code requires just a few 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 knowledge: [Int] = Array(0.. Int {
        knowledge.depend
    }

    func collectionView(
        _ collectionView: UICollectionView,
        cellForItemAt indexPath: IndexPath
    ) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(
            withReuseIdentifier: Cell.identifier, 
            for: indexPath
        ) as! Cell

        let knowledge = 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 {
        .init(width: collectionView.bounds.width, peak: 44)
    }

    func collectionView(
        _ collectionView: UICollectionView,
        format collectionViewLayout: UICollectionViewLayout,
        insetForSectionAt part: Int
    ) -> UIEdgeInsets {
        .init(high: 0, left: 0, backside: 0, proper: 0) //.zero
    }

    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 straightforward. Anchors are actually highly effective, Interface Builder is extraordinarily useful, however typically it’s simply sooner to create your views from code. The selection is yours, however please don’t be afraid of coding person interfaces! 😅

Associated posts


· 6 min learn


On this nice iOS Auto Structure tutorial I will educate you tips on how to assist rotation, use constraints, work with layers, animate nook radius.


· 2 min learn


Be taught in regards to the initialization strategy of the 2 well-known courses in UIKit. Say whats up to UIViewcontroller, and UIView init patterns.


· 5 min learn


Do you wish to discover ways to load a xib file to create a customized view object? Properly, this UIKit tutorial is only for you written in Swift.


· 3 min learn


Discover ways to sync recordsdata and knowledge by way of a shared iCloud drive folder utilizing the most recent model of Swift programming language.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments