繁体   English   中英

UICollectionview重用现有单元格

[英]UICollectionview Reuse Existing cell

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

任何人都可以帮助我避免此问题,请在下面找到我的代码,

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()

}

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

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()
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM