How to implement UICollectionView in Swift with code only

class SampleCollectionView: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    lazy var collectionView : UICollectionView = {
        let collectionView = UICollectionView(

            frame: CGRect(x: 0, y: statusBarHeight, width: self.view.frame.width, height: self.view.frame.size.height - statusBarHeight),
            collectionViewLayout: UICollectionViewFlowLayout())
            collectionView.delegate = self
            collectionView.dataSource = self
            collectionView.register(CollectionViewCell.self, forCellWithReuseIdentifier: "cellId")

        return collectionView
    }()

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        //Number of items in the cell
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        //Item size
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        //Top, bottom, left, and right margins of the entire item display area
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        //Minimum top and bottom margins for items
    }
    //Item display content (requires UICollectionViewDataSource)
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! CollectionViewCell
        //What is displayed in the cell
        return cell
    }
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        //Action when a cell is clicked
    }
}

Recommended Posts

How to implement UICollectionView in Swift with code only
[Swift] I tried to implement Instagram profile-like UI with UICollectionView only with code without storyboard
Implement Swift UITextField in code
How to implement infinite scrolling (page nate) in Swift TableView
[Swift5] How to avoid applying dark mode (dark appearance) in code
[swift5] How to specify color in hexadecimal
How to implement search functionality in Rails
How to implement date calculation in Java
How to implement Kalman filter in Java
[Swift] How to fix Label in UIPickerView
How to call Swift 5.3 code from Objective-C
[Swift] Easy to implement modal with PanModal
[Swift] How to implement the countdown function
How to implement coding conventions in Java
Only this! How to prevent forgetting code format in team development
How to implement TextInputLayout with validation function
[Swift5] How to implement standby screen using'PKHUD'
How to implement ranking functionality in Rails
[Swift5] How to implement animation using "lottie-ios"
How to implement asynchronous processing in Outsystems
How to overwrite Firebase data in Swift
[swift5] How to change the color of TabBar or the color of item of TabBar with code
How to color code console output in Eclipse
[Swift] How to implement the LINE login function
[swift5] How to implement the Twitter share function
How to add sound in the app (swift)
[Swift] How to link the app with Firebase
How to implement a like feature in Rails
[Swift] How to implement the fade-in / out function
How to write test code with Basic authentication
[Rails] How to easily implement numbers with pull-down
How to implement optimistic locking in REST API
How to execute tasks in parallel in Swift in Swift Package
How to implement Pagination in GraphQL (for ruby)
Implement CustomView in code
How to get the ID of a user authenticated with Firebase in Swift
How to embed JavaScript variables in HTML with Thymeleaf
How to sort in ascending / descending order with SQLite
How to call functions in bulk with Java reflection
How to implement guest login in 5 minutes in rails portfolio
How to implement a like feature in Ajax in Rails
How to switch Tomcat context.xml with WTP in Eclipse
How to use Z3 library in Scala with Eclipse
Implement the UICollectionView of iOS14 with the minimum required code.
Organized how to interact with the JDK in stages
How to display a browser preview in VS Code
[How to insert a video in haml with Rails]
How to delete untagged images in bulk with Docker
How to use JDD library in Scala with Eclipse
How to build Java development environment with VS Code
How to query Array in jsonb with Rails + postgres
Summary of how to implement default arguments in Java
Code to use when you want to process Json with only standard library in Java
How to write good code
[Beginner] Implement NavigationBar in code
[Swift] How to use UserDefaults
How to use Swift UIScrollView
How to number (number) with html.erb
How to update with activerecord-import
[Rails] How to implement scraping
[Java] How to implement multithreading