【问题标题】:UICollectionview Reuse Existing cellUICollectionview 重用现有单元格
【发布时间】:2016-11-22 09:08:21
【问题描述】:

我想生成具有 100 个单元格的集合视图,但它不应该在每次滚动时都重新分配,但在我的代码中它总是新创建单元格。

任何人帮助我避免这个问题,请在下面找到我的代码,

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
    return colorArray.count        
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    let colleCell: colorCell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! colorCell

    colleCell.bgColor.backgroundColor = UIColor(red: colorArray[indexPath.row].valueForKey("Red") as! CGFloat/255, green: colorArray[indexPath.row].valueForKey("Green") as! CGFloat/255, blue: colorArray[indexPath.row].valueForKey("Blue") as! CGFloat/255, alpha: 1.0)

    return colleCell
}

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath)
{
    let cell:colorCell = collectionView.cellForItemAtIndexPath(indexPath) as! colorCell

    cell.layer.borderWidth = 2.0
    cell.layer.borderColor = UIColor.whiteColor().CGColor

    selectedColor = indexPath.row

    sampleColorView.backgroundColor = UIColor(red: colorArray[indexPath.row].valueForKey("Red") as! CGFloat/255, green: colorArray[indexPath.row].valueForKey("Green") as! CGFloat/255, blue: colorArray[indexPath.row].valueForKey("Blue") as! CGFloat/255, alpha: 1.0)

    self.view.backgroundColor = UIColor(red: colorArray[indexPath.row].valueForKey("Red") as! CGFloat/255, green: colorArray[indexPath.row].valueForKey("Green") as! CGFloat/255, blue: colorArray[indexPath.row].valueForKey("Blue") as! CGFloat/255, alpha: 0.7)

    sampleColorView.layer.cornerRadius = 75
}



func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath)
{
    let cell:colorCell = collectionView.cellForItemAtIndexPath(indexPath) as! colorCell

    selectedIndex = -1;

    cell.layer.borderWidth = 0.0
    cell.layer.borderColor = UIColor.grayColor().CGColor

    self.view.backgroundColor = UIColor.grayColor()

}

【问题讨论】:

  • 你怎么知道它正在分配新的单元格而不是重复使用它们?
  • 通过在选择单元格后滑动collectionview时知道其单元格边框颜色变化
  • 我只想通过更改单元格边框颜色向用户显示选择了哪个单元格,哪个不是,我该怎么做?
  • cellForItemAtIndexPath中需要将indexPath.item与选中的item进行比较,并根据需要更改边框颜色。
  • 谢谢你能把更新的台词发回给我吗?

标签: ios swift2 uicollectionview ios9 uicollectionviewcell


【解决方案1】:

我认为它被正确地重用了,请仔细检查一下?但我建议设置选定/取消选定单元格样式的方法,您可以覆盖 UICollectionViewCell 的选定属性。

class ColorCell : UICollectionViewCell{
    override var selected: Bool{
        didSet {
            layer.borderWidth = selected ? 2 : 0
            layer.borderColor = selected ? UIColor.whiteColor().CGColor : UIColor.grayColor().CGColor
        }
    /* your code */
}

和 UICollectionViewDelegate 实现:

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
   selectedColor = indexPath.row
   sampleColorView.backgroundColor = /*Color*/
   view.backgroundColor = /*Color*/
   sampleColorView.layer.cornerRadius = 75
}

func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) {
    selectedIndex = -1;
    self.view.backgroundColor = UIColor.grayColor()
}

【讨论】:

  • 感谢您的热情回复,