The collection view created in Swift is almost the same as in OC, mainly implementing the Uicollectionviewdatasource Data source protocol and Uicollectionviewdelegateflowlayout custom layout protocol. Where Uicollectionviewdelegateflowlayout custom layout protocols are inherited from Uicollectionviewdelgate. Use a custom layout to set the size, position, spacing, and so on of the collection View cell
For example:
Let FlowLayout = Uicollectionviewflowlayout ()//Create Layout object
Flowlayout.itemsize = Cgsizemake ((device_width-7)/2, 210)//Set each cell size
Flowlayout.scrolldirection = uicollectionviewscrolldirection.vertical//Set vertical display
Flowlayout.sectioninset = Uiedgeinsetsmake (0, 1, 0, 1)//Set margin
flowlayout.minimumlinespacing = 0.0;//the upper and lower of each adjacent layout
Flowlayout.minimuminteritemspacing = 0.0;//The left and right of each adjacent layout
flowlayout.headerreferencesize = cgsizemake (0, 0);
Specific examples are as follows:
1. Create a project with the name Collection view--swift
2. Import footage, I have imported 9 photos
3. Operation of the code in Viewcontroller.swift:
Declaring attributes and letting classes inherit the protocol
Class The property.
Preparing data
Set up data sources and proxies
Set the collection view cell size when draining a layout
Register cell
To add a collection view to a view
Implementing a Data source protocol
Implementing the Proxy Protocol
Operation Result:
Swift: Create a Collection View Uicollectionview