swift - have a button outside the collection view to do what the collection view cell is supposed to do -
i have collection view acting image carousel when press on cell pdf file open, code below that. wanna have button press on instead of pressing on cell itself. dont know how because of line self.selectedindexpath = indexpath
func collectionview(_ collectionview: uicollectionview, didselectitemat indexpath: indexpath) { self.selectedindexpath = indexpath self.performsegue(withidentifier: "showbook", sender: self) } override func prepare(for segue: uistoryboardsegue, sender: any?) { if segue.identifier == "showbook" { let vc = segue.destination as! showmo2lfatvc vc.book = self.arraybooks[self.selectedindexpath.row] } }
this button function , have tried
@ibaction func openbook(_ sender: any) { // self.selectedindexpath = indexpath // self.performsegue(withidentifier: "showimage", sender: self) // self.selectedindexpath = indexpath }
use **indexpathsforselecteditems**
property of uicollectionview
indexpaths
of selected
uicollectionviewcells
. number of elements depends on whether multiple selection
allowed in uicollectionview
or not.
example:
override func prepare(for segue: uistoryboardsegue, sender: any?) { if segue.identifier == "showbook" { let vc = segue.destination as! viewcontroller2 if let selectedindexpath = self.collectionview.indexpathsforselecteditems?.first //here { vc.book = self.arraybooks[selectedindexpath.row] } } } @ibaction func openbook(_ sender: uibutton) { self.performsegue(withidentifier: "showbook", sender: self) }
no need create separate variable selectedindexpath
keep track of selected cell. call performsegue
on button tap
, rest of work in prepare(for segue: uistoryboardsegue, sender: any?)
Comments
Post a Comment