簡體   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