选择单元格时,在UICollectionViewCell中切换图像

时间:2016-04-18 15:31:48

标签: swift uicollectionview uicollectionviewcell

我有一个项目的UICollectionView,我想在用户选择单元格时切换单元格中的图像。

我有一个自定义的UICollectionViewCell:

class RDCell: UICollectionViewCell {

var textLabel: UILabel!
var imageView: UIImageView!
var isSelected: Bool!

...(do init and all that good stuff)

}

并在集合视图中选择了项目:

func collectionView(collectionView: UICollectionView, shouldSelectItemAtIndexPath indexPath: NSIndexPath) -> Bool {

    let celld = (collectionView.dequeueReusableCellWithReuseIdentifier("collectionCell", forIndexPath: indexPath) as? RDCell)!

    let indexPaths = collectionView.indexPathsForSelectedItems()

    let newCell = collectionView.cellForItemAtIndexPath(indexPath) as! RDCell
    if(celld.selected){

        var image: UIImage = UIImage(named: "notSelected")!
        newCell.imageView.image = image

        newCell.selected = false
    }else{
        var image: UIImage = UIImage(named: "selected")!
        newCell.imageView.image = image

        newCell.selected = true
    }

return true
}

我的尝试部分有效,在选择和取消选择一个项目后我无法再次选择它之后,我需要在返回选择第一个之前选择不同的单元格,并且所有选定的单元格都会发生同样的错误。

任何建议或其他方式来实现我寻求的功能将不胜感激。

4 个答案:

答案 0 :(得分:2)

  

Swift 4

我只是在collectionView的“cellForItemAt indexPath”方法中使用属性“highlightedImage”,并在其上设置另一个图像。

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)

    cell.backgroundColor = UIColor.clear

    let imageView = cell.viewWithTag(1) as! UIImageView
    imageView.image = imagesArray1[indexPath.row]
    imageView.highlightedImage = imagesArray2[indexPath.row]
    imageView.contentMode = .scaleAspectFill
    return cell
}

在我的情况下,我在imageView中分配了一个标签并通过它调用它。

最好的问候。

答案 1 :(得分:1)

你应该从不直接调用cellForItemAtIndexPath。您无法保证您获得的单元格,并且您所做的更改可能无效。

执行此操作的正确方法是跟踪类中的状态并更改cellForItemAtIndexPath中单元格的状态。然后,当您需要更新视图时,只需致电collectionView?.reloadData()

参考的简单示例:

var selectionTracking: [[Bool]] = []


func collectionView(collectionView: UICollectionView, shouldSelectItemAtIndexPath indexPath: NSIndexPath) -> Bool {

    if let selected = selectionTracking[indexPath.section][indexPath.row] {
        return selected
    }
    else{
        selectionTracking[indexPath.section][indexPath.row] = false
    }
    return true
}

override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

    if let selected = selectionTracking[indexPath.section][indexPath.row] {
        selectionTracking[indexPath.section][indexPath.row] = !selectionTracking[indexPath.section][indexPath.row]
    }
    else{
        selectionTracking[indexPath.section][indexPath.row] = true
    }

    collectionView?.reloadData()
    return true
}

答案 2 :(得分:0)

我的方法会有所不同,我会使用collectionView的didDeselectItemAt方法...

斯威夫特3:

addSheet()

答案 3 :(得分:0)

迅速4/5:

在您的 collectionViewCell类中定义一个名为 imageName 的变量 然后 override isSelected 属性可根据所选状态设置图像名称,并在 collectionView cellForItemAt 方法中为每个单元格设置imageName变量的值。

CollectionViewCell类:

class YourCollectionViewCell: UICollectionViewCell
{

    @IBOutlet var cellIcon: UIImageView!

    var imageName = ""

    override var isSelected: Bool {
        didSet {
            if self.isSelected {
                self.cellIcon.image = UIImage(named: "\(self.imageName) Highlighted")
            } else {
                self.cellIcon.image = UIImage(named: "\(self.imageName) Unhighlighted")
            }

        }
    }
}

CollectionView数据源:

class YourCollectionVewController: UICollectionViewDataSource 
{

     func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        // this line is an extension don't copy paste
        let cell = collectionView.dequeueReusableCell(with: YourCollectionViewCell.self, for: indexPath) 

        let imageName = "yourImageName Unhighlighted"
        cell.cellIcon.image = UIImage(named: imageName)
        cell.imageName = imageName

        return cell
    }
}
相关问题