ios - didSelectItemAtIndexPath modifying multiple cells in UICollectionView -
i have collection view 30 items, , want perform on press. in way:
func collectionview(collectionview: uicollectionview, didselectitematindexpath indexpath: nsindexpath) { let cell = collectionview.cellforitematindexpath(indexpath) as! itemcell var translucentview = iltranslucentview(frame: cgrectmake(0, 0, cell.contentview.frame.size.width, cell.contentview.frame.size.height)) translucentview.translucentalpha = 0.9 translucentview.translucentstyle = uibarstyle.blacktranslucent translucentview.translucenttintcolor = uicolor.clearcolor() translucentview.backgroundcolor = uicolor.clearcolor() translucentview.alpha = 0.0 cell.contentview.addsubview(translucentview) uiview.animatewithduration(0.4, animations: { translucentview.alpha = 1.0 }) }
the function works expected, view appears not on tapped cell, on cell in same position not visible on screen. if there 3 visible cells on screen , tap on number 1, when scroll view has been added cell 4, 7, etc...
uicollectionview
re-use cells uitableview
. when cell scrolled offscreen added queue, , re-used next cell scrolled onscreen(e.g. cell 4, 7 ...).
simply removing translucentview
solve issue:
uiview.animatewithduration(0.4, animations: { () -> void in translucentview.alpha = 1.0 }) { (finish) -> void in translucentview.removefromsuperview() }
you can create translucentview
in itemcell
, update status in cellforitematindexpath
method:
override func collectionview(collectionview: uicollectionview, cellforitematindexpath indexpath: nsindexpath) -> uicollectionviewcell { let cell = collectionview.dequeuereusablecellwithreuseidentifier(cellidentifier, forindexpath: indexpath) as! itemcell if find(collectionview.indexpathsforselecteditems() as! [nsindexpath], indexpath) != nil { cell.translucentview.alpha = 1.0 } else { cell.translucentview.alpha = 0.0 } return cell } override func collectionview(collectionview: uicollectionview, didselectitematindexpath indexpath: nsindexpath) { let cell = collectionview.cellforitematindexpath(indexpath) as! itemcell uiview.animatewithduration(0.4, animations: { cell.translucentview.alpha = 1.0 }) } override func collectionview(collectionview: uicollectionview, diddeselectitematindexpath indexpath: nsindexpath) { let cell = collectionview.cellforitematindexpath(indexpath) as! itemcell uiview.animatewithduration(0.4, animations: { cell.translucentview.alpha = 0.0 }) }
Comments
Post a Comment