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