我有一个UICollectionView,在点击时在其中为单元格提供了蓝色边框。问题是,当我向下滚动到更多单元格时,某些新单元格会模仿蓝色边框,而不会被点击。有人知道如何解决这个问题吗?

码:

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

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell: OffersCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("cell4", forIndexPath: indexPath) as! OffersCollectionViewCell
    tradeImages[indexPath.row].getDataInBackgroundWithBlock{
        (imageData: NSData?, error: NSError?) -> Void in
        if error == nil {
            let image = UIImage(data: imageData!)
            cell.offersImg.image = image
        }
    }
    return cell
}

func collectionView(collectionView: UICollectionView, shouldSelectItemAtIndexPath indexPath: NSIndexPath) -> Bool {
    var cell = self.collectionView.cellForItemAtIndexPath(indexPath)
    if cell!.tag == 0 {
        cell!.layer.borderWidth = 2.0
        cell!.layer.borderColor = UIColor.blueColor().CGColor
        offersArray.append(objectsArray[indexPath.row] as! PFObject)
        cell!.tag = 1
    } else {
        cell!.layer.borderWidth = 0.0
        cell!.tag = 0
        if let index = find(offersArray, objectsArray[indexPath.row] as! PFObject) {
            offersArray.removeAtIndex(index)
        }
    }
    return true
}

最佳答案

prepareForReuse()中使用方法OffersCollectionViewCell并在那里设置单元格的初始值以供重新使用。

例如:

override func prepareForReuse() {
    super.prepareForReuse()

    layer.borderWidth = 0.0
    layer.borderColor = UIColor.whiteColor().CGColor

    // Other initial values
}

关于ios - 如何停止重用单元格模仿其他单元格的样式更改?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30736876/

10-14 21:16
查看更多