ios - Swift: Hide button in UICollectionView cell -


i programmatically creating cells , adding delete button each 1 of them. problem i'd toggle .hidden state. idea have edit button toggles of button's state @ same time. maybe going wrong way?

func collectionview(collectionview: uicollectionview, cellforitematindexpath indexpath: nsindexpath) -> uicollectionviewcell {     let cell = collectionview.dequeuereusablecellwithreuseidentifier("verticalcell", forindexpath: indexpath) as! racollectionviewcell     let slide = panelslides[indexpath.row]     cell.slidedata = slide     cell.slideimageview.setimagewithurl(nsurl(string: image_url + slide.imagename + ".jpg")!)     cell.setneedslayout()      let image = uiimage(named: "ic_close") uiimage?     var deletebutton = uibutton(type: uibuttontype.custom) uibutton     deletebutton.frame = cgrectmake(-25, -25, 100, 100)     deletebutton.setimage(image, forstate: .normal)     deletebutton.addtarget(self,action:#selector(deletecell), forcontrolevents:.touchupinside)     deletebutton.hidden = editon     cell.addsubview(deletebutton)     return cell }   @ibaction func editbuttontap(sender: anyobject) {     editon = !editon     sidepanelcollectionview.reloaddata() } 

i think want iterate on of data index , call cellforitematindexpath: on uicollectionview each index. can take existing cell, cast specific type as! racollectionviewcell set button hidden values way.

example (apologies i'm not in xcode verify precisely right gist):

for (index, data) in mydataarray.enumerate() {    let cell = collectionview.cellforrowatindexpath(nsindexpath(row: index, section: 0)) as! racollectionviewcell    cell.deletebutton.hidden = false } 

you need sort of isediting boolean variable in view controller keeps track of fact in editing state scroll, newly configured cells continue display with/without button. going need existing code above make sure continues work scrolling occurs. instead of creating new delete button every time, should put button in storyboard , set reference , can use cell.deletebutton.hidden = !isediting


Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -