Close Menu
  • Home
  • AI
  • Big Data
  • Cloud Computing
  • iOS Development
  • IoT
  • IT/ Cybersecurity
  • Tech
    • Nanotechnology
    • Green Technology
    • Apple
    • Software Development
    • Software Engineering

Subscribe to Updates

Get the latest technology news from Bigteetechhub about IT, Cybersecurity and Big Data.

    What's Hot

    3D-Printed Cinema Film Camera Oozes Vintage Vibes

    December 28, 2025

    Probing the fundamental nature of the Higgs Boson – Physics World

    December 28, 2025

    MIT Technology Review’s most popular stories of 2025

    December 28, 2025
    Facebook X (Twitter) Instagram
    Facebook X (Twitter) Instagram
    Big Tee Tech Hub
    • Home
    • AI
    • Big Data
    • Cloud Computing
    • iOS Development
    • IoT
    • IT/ Cybersecurity
    • Tech
      • Nanotechnology
      • Green Technology
      • Apple
      • Software Development
      • Software Engineering
    Big Tee Tech Hub
    Home»iOS Development»UICollectionView data source and delegates programmatically
    iOS Development

    UICollectionView data source and delegates programmatically

    big tee tech hubBy big tee tech hubJune 22, 20250583 Mins Read
    Share Facebook Twitter Pinterest Copy Link LinkedIn Tumblr Email Telegram WhatsApp
    Follow Us
    Google News Flipboard
    UICollectionView data source and delegates programmatically
    Share
    Facebook Twitter LinkedIn Pinterest Email Copy Link


    6/26/18 4:20 PM
    · 1 min read


    In this quick UIKit tutorial I’ll show you how to create a simple UICollectionView without Interface Builder, but only using Swift.

    UICollectionViewCell programmatically

    If you’d like to add views to your cell, you should use the init(frame:) method, and set up your view hierarchy there. Instead of awakeFromNib you should style your views in the init method as well. You can reset everything inside the usual prepareForReuse method. As you can see by using anchors sometimes it’s worth to ditch IB entirely. 🎉

    class Cell: UICollectionViewCell {
    
        static var identifier: String = "Cell"
    
        weak var textLabel: UILabel!
    
        override init(frame: CGRect) {
            super.init(frame: frame)
    
            let textLabel = UILabel(frame: .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 implemented")
        }
    
        override func prepareForReuse() {
            super.prepareForReuse()
            
            reset()
        }
    
        func reset() {
            textLabel.textAlignment = .center
        }
    }
    

    UICollectionView programmatically

    Creating collection view controllers using only Swift code requires only a few additional lines. You can implement loadView and create your UICollectionView object there. Store a weak reference of it inside the controller, and the rest is the same.

    class ViewController: UIViewController {
    
        weak var collectionView: UICollectionView!
    
        var data: [Int] = Array(0..<10)
    
        override func loadView() {
            super.loadView()
    
            let collectionView = UICollectionView(
                frame: .zero, 
                collectionViewLayout: UICollectionViewFlowLayout()
            )
            collectionView.translatesAutoresizingMaskIntoConstraints = false
            view.addSubview(collectionView)
            NSLayoutConstraint.activate([
                view.topAnchor.constraint(equalTo: collectionView.topAnchor),
                view.bottomAnchor.constraint(equalTo: collectionView.bottomAnchor),
                view.leadingAnchor.constraint(equalTo: collectionView.leadingAnchor),
                view.trailingAnchor.constraint(equalTo: collectionView.trailingAnchor),
            ])
            collectionView = collectionView
        }
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            collectionView.dataSource = self
            collectionView.delegate = self
            collectionView.register(Cell.self, forCellWithReuseIdentifier: Cell.identifier)
            collectionView.alwaysBounceVertical = true
            collectionView.backgroundColor = .white
        }
    }
    
    extension ViewController: UICollectionViewDataSource {
    
        func collectionView(
            _ collectionView: UICollectionView,
            numberOfItemsInSection section: Int
        ) -> Int {
            data.count
        }
    
        func collectionView(
            _ collectionView: UICollectionView,
            cellForItemAt indexPath: IndexPath
        ) -> UICollectionViewCell {
            let cell = collectionView.dequeueReusableCell(
                withReuseIdentifier: Cell.identifier, 
                for: indexPath
            ) as! Cell
    
            let data = data[indexPath.item]
            cell.textLabel.text = String(data)
            return cell
        }
    }
    
    extension ViewController: UICollectionViewDelegate {
    
        func collectionView(
            _ collectionView: UICollectionView, 
            didSelectItemAt indexPath: IndexPath
        ) {
    
        }
    }
    
    extension ViewController: UICollectionViewDelegateFlowLayout {
    
        func collectionView(
            _ collectionView: UICollectionView,
            layout collectionViewLayout: UICollectionViewLayout,
            sizeForItemAt indexPath: IndexPath
        ) -> CGSize {
            .init(width: collectionView.bounds.width, height: 44)
        }
    
        func collectionView(
            _ collectionView: UICollectionView,
            layout collectionViewLayout: UICollectionViewLayout,
            insetForSectionAt section: Int
        ) -> UIEdgeInsets {
            .init(top: 0, left: 0, bottom: 0, right: 0) //.zero
        }
    
        func collectionView(
            _ collectionView: UICollectionView,
            layout collectionViewLayout: UICollectionViewLayout,
            minimumInteritemSpacingForSectionAt section: Int
        ) -> CGFloat {
            0
        }
    
        func collectionView(
            _ collectionView: UICollectionView,
            layout collectionViewLayout: UICollectionViewLayout,
            minimumLineSpacingForSectionAt section: Int
        ) -> CGFloat {
            0
        }
    }
    

    That was easy. Anchors are really powerful, Interface Builder is extremely helpful, but sometimes it’s just faster to create your views from code. The choice is yours, but please don’t be afraid of coding user interfaces! 😅


    UICollectionView data source and delegates programmatically

    Share this article

    Thank you. 🙏

    Related posts

    UICollectionView data source and delegates programmatically

    2/3/22 4:20 PM
    · 8 min read


    In this article I’ve gathered my top 10 favorite modern UIKit tips that I’d definitely want to know before I start my next project.

    UICollectionView data source and delegates programmatically

    5/23/19 4:20 PM
    · 5 min read


    Learn how to build complex forms with my updated collection view view-model framework without the struggle using Swift.

    UICollectionView data source and delegates programmatically

    10/16/18 4:20 PM
    · 5 min read


    Do you want to learn how to load a xib file to create a custom view object? Well, this UIKit tutorial is just for you written in Swift.

    UICollectionView data source and delegates programmatically

    10/21/19 4:20 PM
    · 4 min read


    Just a little advice about creating custom view programmatically and the truth about why form building with collection views sucks.



    Source link

    Data delegates programmatically source UICollectionView
    Follow on Google News Follow on Flipboard
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email Copy Link
    tonirufai
    big tee tech hub
    • Website

    Related Posts

    ios – Background Assets Framework server connection problem

    December 27, 2025

    ios – SwiftUI-Button: Complete VStack clickable

    December 26, 2025

    ios – ld: framework ‘Pods_MyProjectName’ not found

    December 25, 2025
    Add A Comment
    Leave A Reply Cancel Reply

    Editors Picks

    3D-Printed Cinema Film Camera Oozes Vintage Vibes

    December 28, 2025

    Probing the fundamental nature of the Higgs Boson – Physics World

    December 28, 2025

    MIT Technology Review’s most popular stories of 2025

    December 28, 2025

    Santa Claus doesn’t exist (according to AI) • Graham Cluley

    December 28, 2025
    About Us
    About Us

    Welcome To big tee tech hub. Big tee tech hub is a Professional seo tools Platform. Here we will provide you only interesting content, which you will like very much. We’re dedicated to providing you the best of seo tools, with a focus on dependability and tools. We’re working to turn our passion for seo tools into a booming online website. We hope you enjoy our seo tools as much as we enjoy offering them to you.

    Don't Miss!

    3D-Printed Cinema Film Camera Oozes Vintage Vibes

    December 28, 2025

    Probing the fundamental nature of the Higgs Boson – Physics World

    December 28, 2025

    Subscribe to Updates

    Get the latest technology news from Bigteetechhub about IT, Cybersecurity and Big Data.

      • About Us
      • Contact Us
      • Disclaimer
      • Privacy Policy
      • Terms and Conditions
      © 2025 bigteetechhub.All Right Reserved

      Type above and press Enter to search. Press Esc to cancel.