Swift: Create a Collection View Uicollectionview

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.