ios - Change image on select within tableview cell - Swift -


i trying change image within cell when selected. here have in view controller:

func tableview(tableview: uitableview, didselectrowatindexpath indexpath: nsindexpath) {  let cell = self.tableview.dequeuereusablecellwithidentifier("cell", forindexpath: indexpath) as! customtableviewcell          cell.bgimage.image = uiimage(named: "second_image")  } 

this set image:

func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell {  let cell = self.tableview.dequeuereusablecellwithidentifier("cell", forindexpath: indexpath) as! customtableviewcell  cell.bgimage.image = uiimage(named: "first_image")  } 

when select cell, image not update. how work?

the issue calling dequeuereusablecellwithidentifier. try using cellforrowatindexpath instead (documentation here). give reference current cell, rather reference new reusable cell.

so, according code above:

let cell = self.tableview.cellforrowatindexpath(indexpath) as! customtableviewcell

this typecast critical because cellforrowatindexpath returns uitableviewcell, not have bgimage member.

also, make sure have member called bgimage uiimageview in customtableviewcell class adding @iboutlet strong var bgimage: uiimageview!, , said iboutlet connected in storyboard/xib.


Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -