ColectionViewController Example iOS swift

Solution:

The below code will explain you how to create collectionview in swift

Code:

import UIKit


class MyViewController: UICollectionViewController {

    

    let reuseIdentifier = "viewcell"

    let items = ["item 1", "item 2", "item 3", "item 4", "item 5"]

    

    override func viewDidLoad() {

        super.viewDidLoad()

        

        collectionView.backgroundColor = .white

        collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)

    }

    

    // MARK: UICollectionViewDataSource

    

    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        return items.count

    }

    

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath)

        cell.backgroundColor = .red

        return cell

    }

    

}

Comments

Popular posts from this blog

Invalid bundle error while upload the app to the app Store

store cgpoint in userdefaults iOS swift