ios - get indexpath from collectionView, sometime get the wrong indexPath -
in uitableview
can use code indexpath
uitableview
extension uiview{ func getbuttonindexpath(tableview:uitableview) -> nsindexpath{ let buttonoriginintableview = self.convertpoint(cgpointzero, toview: tableview) return tableview.indexpathforrowatpoint(buttonoriginintableview)! } }
but doesn't work in uicollectionview
extension uiview{ func getindexpath(uc:uicollectionview) -> nsindexpath?{ let senderp = cgpointmake(0, self.frame.height / 2) let point : cgpoint = self.convertpoint(senderp, toview:uc) let ind = uc.indexpathforitematpoint(point) print("what click is", point, ind) return ind }
i put in button of cell in uicollectionview
delbtn.addtarget(self, action: #selector(favviewcontroller.delfavaction(_:)), forcontrolevents: .touchupinside)
and function
func delfavaction(sender:uibutton){ guard let indexpath = sender.getindexpath(self.collectionview) else { return } print(indexpath) }
but wrong indexpath.item
, how happening?
is code?
func collectionview(collectionview: uicollectionview, layout collectionviewlayout: uicollectionviewlayout, sizeforitematindexpath indexpath: nsindexpath) -> cgsize { return rectarr[indexpath.item].size }
you write line:
delbtn.addtarget(self, action: #selector(favviewcontroller.delfavaction(_:)), forcontrolevents: .touchupinside)
below write other line:
delbtn.tag = indexpath.item;
and item:
func delfavaction(sender:uibutton){ // sender.tag convert uibutton; // objectvie c write: uibutton *btn = (uibutton *)sender; }
Comments
Post a Comment