[Ios]swift Custom Uicollectionviewcell

Source: Internet
Author: User

Just contact with Swift and iOS, not quite understand the logic that leads to some problems. Share the Swift custom Uicollectionviewcell here

First of all my viewcontroller not directly inherit Uicollectionviewcontroller, but add Uicollectionview to my storyboard,

Then create a swift file and let this swift inherit Uicollectionviewcell

Import Foundationclasssvgcell:uicollectionviewcell{convenience required init (coder:nscoder) {self.init (Frame:cgrect (x: 0Y:0, Width: -, Height: -))    }   //required Init (coder:nscoder) {//fatalerror ("Nscoding not supported")//    }    OverrideInit (frame:cgrect) {super.init (frame:frame)}//About Inheritance//http://stackoverflow.com/questions/25126295/swift-class-does-not-implement-its-superclasss-required-members}

When inheriting Uicollectionviewcell here, a parent class initialization method and a required initialization method are required, and a link above is a description of the required method, which is necessary for the new version, otherwise it will report a syntax error. The error content is probably asking you to implement a required method.

It is not possible to write a custom initialization here because this cell is not created by Init.

And go back to our viewcontroller.

Import UIKit class Viewcontroller:uiviewcontroller, Uicollectionviewdatasource,uicollectionviewdelegate {//inherit the following two protocols, You need to use CollectionView and then you can make a copy of the following three      required methods var svgcollection:uicollectionview!

Let reuseidentifier="Svgcell"

func CollectionView (Collectionview:uicollectionview!, Numberofitemsinsection Section:int)Int {return 2    }//These 3 func inherit the DataSource & delegate must be rewritten, if there are multiple TableView or collection view in the inside use judgment, the parameter CollectionView judgefunc CollectionView (Collectionview:uicollectionview!, Cellforitematindexpath indexpath:nsindexpath!) uicollectionviewcell! {                varCell:svgcell=svgcollection.dequeuereusablecellwithreuseidentifier (Reuseidentifier, Forindexpath:indexpath) asSvgcell//Reuse//This cell here doesn't need to be initialized .//construct a cell before return if it is initialized again, it will lead to Uncatch's exception, the content is probably not reuseidentifier        returncell}//func Numberofsectionsincollectionview (Collectionview:uicollectionview!) -Int {return 1    }//}

On the inside of the uicollectionview that we added to our storyboard.

The cell inside this column sets its properties

Setting the identifier property is basically done, then you can write the code you need in the custom Uiviewcollectioncell class.

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.