ios - How to add different image for different cell with different text in UICollectionView programmatically? -
i want add different image different cell different textlabel
in uicollectionview
programmatically. tried many way show error.
here code:
import foundation import uikit class homecvc: uicollectionviewcontroller, uicollectionviewdelegateflowlayout { let homecellid = "homeid" override func viewdidload() { super.viewdidload() // homeobjects = homeobject.sampleobjectheadline() navigationitem.title = "home" collectionview?.backgroundcolor = .green collectionview?.register(homecell.self, forcellwithreuseidentifier: homecellid) } override func collectionview(_ collectionview: uicollectionview, numberofitemsinsection section: int) -> int { return 7 } override func collectionview(_ collectionview: uicollectionview, cellforitemat indexpath: indexpath) -> uicollectionviewcell { let cell = collectionview.dequeuereusablecell(withreuseidentifier: homecellid, for: indexpath) as! homecell return cell } func collectionview(_ collectionview: uicollectionview, layout collectionviewlayout: uicollectionviewlayout, sizeforitemat indexpath: indexpath) -> cgsize { return cgsize(width: view.frame.width, height: 150) } func collectionview(_ collectionview: uicollectionview, layout collectionviewlayout: uicollectionviewlayout, minimumlinespacingforsectionat section: int) -> cgfloat { return 0 } } class homecell: uicollectionviewcell { override init(frame: cgrect){ super.init(frame: frame) homeimageviewsetup() } let homeimageview: uiimageview = { let imageview = uiimageview() imageview.backgroundcolor = .blue imageview.image = uiimage(named: "imagename") // imageview.contentmode = .scaleaspectfill // imageview.clipstobounds = true imageview.layer.cornerradius = 5 imageview.layer.maskstobounds = true imageview.translatesautoresizingmaskintoconstraints = false return imageview }() let homeimagelabel: uilabel = { let label = uilabel() label.frame = cgrect(x: 20, y: -10, width: 200, height: 80) label.textcolor = .white label.text = "welcome page" return label }() func homeimageviewsetup() { addsubview(homeimageview) addsubview(homeimagelabel) addconstraints(nslayoutconstraint.constraints(withvisualformat: "h:|-5-[v0]-5-|", options: nslayoutformatoptions(), metrics: nil, views: ["v0": homeimageview])) addconstraints(nslayoutconstraint.constraints(withvisualformat: "v:|-5-[v0]|", options: nslayoutformatoptions(), metrics: nil, views: ["v0": homeimageview])) } required init?(coder adecoder: nscoder) { fatalerror("init(coder:) has not been implemented") } }
you can configure homeimagelabel.text
, homeimageview.image
in method: collectionview(_:cellforitemat:)
. example:
override func collectionview(_ collectionview: uicollectionview, cellforitemat indexpath: indexpath) -> uicollectionviewcell { let cell = collectionview.dequeuereusablecell(withreuseidentifier: homecellid, for: indexpath) as! homecell cell.homeimageview.image = uiimage(named: "imagename") cell.homeimagelabel.text = "welcome page" return cell }
you should create array store images , text , retrieve each item using indexpath
.
struct item { var imagename: string var text: string } let data: [item] = [ item(imagename: "imagename", text: "welcome page"), item(imagename: "otherimagename", text: "other text"), ] override func collectionview(_ collectionview: uicollectionview, numberofitemsinsection section: int) -> int { return data.count } override func collectionview(_ collectionview: uicollectionview, cellforitemat indexpath: indexpath) -> uicollectionviewcell { let cell = collectionview.dequeuereusablecell(withreuseidentifier: homecellid, for: indexpath) as! homecell let item = data[indexpath.item] cell.homeimageview.image = uiimage(named: item.imagename) cell.homeimagelabel.text = item.text return cell }
Comments
Post a Comment