[英]didSelectItemAtIndexPath modifying multiple cells in UICollectionView
I have collection view with 30 items, and want to perform something on press. 我有30个项目的收藏视图,并希望在印刷时执行某些操作。 I do it in this 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 as expected, however the view appears not only on the tapped cell, but also on the cell in the same position that is not visible on the screen. 该功能将按预期工作,但是视图不仅会出现在所单击的单元格上,还会出现在屏幕上不可见的相同位置的单元格上。 So if there are 3 visible cells on the screen and I tap on number 1, then when I scroll the view has been added to cell 4, 7, etc...
因此,如果屏幕上有3个可见的单元格,而我点击数字1,那么当我滚动查看时,该视图已添加到单元格4、7等。
UICollectionView
re-use cells like to UITableView
. UICollectionView
重用像UITableView
这样的单元格。 When a cell is scrolled offscreen it is added to queue, and will be re-used for the next cell to be scrolled onscreen(eg cell 4, 7 ...). 当一个单元在屏幕外滚动时,它被添加到队列中,并将被重新用于下一个要在屏幕上滚动的单元(例如,单元4、7 ...)。
Simply removing the translucentView
will solve this issue: 只需删除
translucentView
解决此问题:
UIView.animateWithDuration(0.4, animations: { () -> Void in
translucentView.alpha = 1.0
}) { (finish) -> Void in
translucentView.removeFromSuperview()
}
You can create the translucentView
in the ItemCell
and update its status in the cellForItemAtIndexPath
method: 您可以在
ItemCell
创建translucentView
并在cellForItemAtIndexPath
方法中更新其状态:
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
})
}
did you set your numberOfSectionsInTableView
correctly like that? 像这样正确设置了
numberOfSectionsInTableView
吗?
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.