Swift Uicollectionview Simple to use

Source: Internet
Author: User

Recently to study the layout of the game level interface implementation, simple to do a uicollectionview demo.

First look at the final effect:



Here's how to implement this by adding a Uicollectionview control to the storyboard corresponding Viewcontroller, and then adding a Collectionviewcell


Add a Label control to it



Note that this cell is named after the Defaultcell, so our UI-level work is over.



Code section:

First of all we need to understand two classes, Uicollectionviewdatasource and uicollectionviewdelegate
Uicollectionviewdatasource is responsible for providing the data sources needed to provide the view
Uicollectionviewdelegate is responsible for handling various events of view

Class Mycollectionviewcontroller:uiviewcontroller, Uicollectionviewdatasource, uicollectionviewdelegate{@IBOutlet        Weak Var cv:uicollectionview!            Override Func Viewdidload () {super.viewdidload () Cv.datasource = self cv.delegate = self} Override Func didreceivememorywarning () {super.didreceivememorywarning ()//Dispose of any resources t    Hat can be recreated. }//Implement Uicollectionviewdatasource func CollectionView (Collectionview:uicollectionview, Numberofitemsinsection sectio    N:int), Int {//return record number return 100; }//Implement Uicollectionviewdatasource func CollectionView (Collectionview:uicollectionview, Cellforitematindexpath Inde   Xpath:nsindexpath)-Uicollectionviewcell {//Return cell contents, where we use the newly created Defaultcell as the display content var Cell:mycolletioncell = Cv.dequeuereusablecellwithreuseidentifier ("Defaultcell", Forindexpath:indexpath) as! Mycolletioncell cell.label.text = "\ (indexpath.section): \ (indexpath.row) "return cell; }//Implement Uicollectionviewdatasource func CollectionView (Collectionview:uicollectionview, Didselectitematindexpath Indexpath:nsindexpath) {//A cell is selected for event handling}}

Then you can see the effect.

Swift Uicollectionview Simple to use

Related Article

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.