简体   繁体   English

didSelectItemAtIndexPath修改UICollectionView中的多个单元格

[英]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.

相关问题 UICollectionView不调用didSelectItemAtIndexPath - UICollectionView not calling didSelectItemAtIndexPath UICollectionView didSelectItemAtIndexPath滚动 - UICollectionView didSelectItemAtIndexPath Scrolling didSelectItemAtIndexPath方法不在UICollectionView中工作 - didSelectItemAtIndexPath method not working in a UICollectionView UICollectionView didSelectitematIndexPath委托吗? - UICollectionView didselectitematindexpath delegate? 未调用 UICollectionView 的 didSelectItemAtIndexPath - didSelectItemAtIndexPath of UICollectionView is not called Swift UICollectionView:didSelectItemAtIndexPath没有响应 - Swift UICollectionView : didSelectItemAtIndexPath not responding 在 UICollectionView 中选择多个单元格 - Selecting multiple cells in a UICollectionView UICollectionView中的多个单元格 - multiple cells in UICollectionView 为什么UICollectionView在滚动其单元格的scrollView后不再调用didSelectItemAtIndexPath? - why UICollectionView not calling didSelectItemAtIndexPath any more after scrolling the scrollView of it's cells? 在UICollectionViewDelegate#collectionView:didSelectItemAtIndexPath中调用UICollectionView#reloadData:隐藏所有单元格 - Calling UICollectionView#reloadData in UICollectionViewDelegate#collectionView:didSelectItemAtIndexPath: hides all cells
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM