swift - How to add two collection view cell by indexPath? -
lets got 4 collection view cells :
if going replace 2 collection view cell @ cell 3 , cell 4 position :
as can see,cell 3 , cell 4 goes down , new cell 1 , cell 2 replace position.
is there way can @ uicollectionview?
i beginner @ using uicollectionview
because app development full of tableview usage.so,i want learn more.
any code appreciated.github repo of example more appreciated.
thanks.
from here took sample project collection view , did modification in per requirement.
and below complete code updating collection array when user press button on view.
import uikit class viewcontroller: uiviewcontroller, uicollectionviewdelegateflowlayout, uicollectionviewdatasource, customcollectionviewcelldelegate { @iboutlet weak var overlayview: uiview! @iboutlet weak var collectionview: uicollectionview! @iboutlet weak var overlaylabel: uilabel! var purchasecount:float = 0.0 //your collection view data source @ start. var cellarray = ["cell 1", "cell 2", "cell 3", "cell 4"] override func viewdidload() { super.viewdidload() let layout: uicollectionviewflowlayout = uicollectionviewflowlayout() layout.sectioninset = uiedgeinsets(top: 50, left: 25, bottom: 50, right: 25) layout.itemsize = cgsize(width: 100, height: 100) collectionview!.collectionviewlayout = layout collectionview!.datasource = self collectionview!.delegate = self collectionview!.registerclass(collectionviewcell.self, forcellwithreuseidentifier: "collectionviewcell") collectionview!.backgroundcolor = uicolor.blackcolor() } func numberofsectionsincollectionview(collectionview: uicollectionview) -> int { return 1 } func collectionview(collectionview: uicollectionview, numberofitemsinsection section: int) -> int { return cellarray.count } func collectionview(collectionview: uicollectionview, cellforitematindexpath indexpath: nsindexpath) -> uicollectionviewcell { let cell = collectionview.dequeuereusablecellwithreuseidentifier("collectionviewcell", forindexpath: indexpath) as! collectionviewcell cell.backgroundcolor = uicolor.blackcolor() cell.textlabel?.text = cellarray[indexpath.row] cell.imageview?.image = uiimage(named: "circle") cell.imageviewstar?.image = uiimage(named: "star") cell.delegate = self return cell } //action when user press button view @ibaction func addnewcell(sender: anyobject) { //insert element collection array. cellarray.insert("new cell 1", atindex: 2) cellarray.insert("new cell 2", atindex: 3) //reload collection view. self.collectionview.reloaddata() } // mark: customcollectionviewcelldelegate func starimagehit(price:float) { print("star image hit in view controller") self.purchasecount = self.purchasecount + price self.overlayview.hidden = false self.overlayview.alpha = 1.0 self.overlaylabel.text = "\(self.purchasecount)" uiview.animatewithduration(1.0, delay: 0.0, options: uiviewanimationoptions.curveeaseout, animations: { self.overlayview.alpha = 0.0 self.view.layoutifneeded() }, completion: nil) } }
and result:
and here sample project you.
Comments
Post a Comment