如何在 Swift 中访问 UICollectionView 中离屏但现有的单元格?

问题描述 投票:0回答:2

标题可能有点难以理解,但是这种情况可能会对你有所帮助。

我正在编写一个多图像选择器。假设限制为 3 张图片,当用户选择了 3 张后,所有其他图片都会有

alpha = 0.3
表示该图片不可选。 (一直向下滚动查看演示)

首先,这是我的代码:

PickerPhotoCell(自定义集合视图单元格):

class PickerPhotoCell: UICollectionViewCell {
    @IBOutlet weak var imageView: UIImageView!

    var selectable: Bool {
        didSet {
            self.alpha = selectable ? 1 : 0.3
        }
    }
}

照片选择器视图控制器:

class PhotoPickerViewController: UICollectionViewController {

    ...
    var photos: [PHAsset]()    // Holds all photo assets
    var selected: [PHAsset]()    // Holds all selected photos
    var limit: Int = 3

    override func viewDidLoad() {
        super.viewDidLoad()

        // Suppose I have a func that grabs all photos from photo library
        photos = grabAllPhotos()
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell ...
        let asset = photos[indexPath.row]

        ...

        // An image is selectable if:
        // 1. It's already selected, then user can deselect it, or
        // 2. Number of selected images are < limit
        cell.selectable = cell.isSelected || selected.count < limit

        return cell
    }

    override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let cell = collectionView.cellForItem(at: indexPath) as! PickerPhotoCell

        if cell.isSelected {
            // Remove the corresponding PHAsset in 'selected' array
        } else {
            // Append the corresponding PhAsset to 'selected' array
        }

        // Since an image is selected/deselected, I need to update
        // which images are selectable/unselectable now
        for visibleCell in collectionView.visibleCells {
            let visiblePhoto = visibleCell as! PickerPhotoCell
            visiblePhoto.selectable = visiblePhoto.isSelected || selected.count < limit
        }
    }
}

这几乎完美,除了一件事,看看 GIF:

问题是

在我选择了 3 张照片后,所有其他可见照片都有

alpha = 0.3
,但是当我再向下滚动一点时,有些照片仍然有
alpha = 1
。我知道为什么会发生这种情况 - 因为它们在屏幕外,调用
collectionView.visibleCells
不会影响它们,并且与其他不存在的单元格不同,即使它们在屏幕外,它们也确实存在。所以我想知道如何访问它们并使它们不可选择?

ios swift uicollectionview
2个回答
1
投票

问题是你试图通过这样做来将你的状态存储在单元格本身中:

if cell.isSelected...
。集合视图中没有离屏单元格,它始终重复使用单元格,您实际上应该在
prepareForReuse
方法中重置单元格的状态。这意味着您需要将数据存储在 UICollectionViewCell 之外。 您可以做的是将选定的
IndexPath
存储在视图控制器的属性中,并使用该数据来标记您的单元格
selected
或不标记。

伪代码:

class MyViewController {

    var selectedIndexes = [IndexPath]()

    func cellForItem(indexPath) {
       cell.isSelected =  selectedIndexes.contains(indexPath)
    }

    func didSelectCell(indexPath) {
        if selectedIndexes.contains(indexPath) {
            selectedIndexes.remove(indexPath)
        } else if selectedIndexes.count < limiit {
            selectedIndexes.append(indexPath)
        }
    }
}

0
投票

您可以使用

collectionView(_:willDisplay:forItemAt:)
来设置这些讨厌的稍微离屏幕外的单元格的状态(这些单元格已在
collectionView(_:cellForItemAt:)
内部实例化,但尚未包含在
visibleCells
中)。

来自苹果文档:

集合视图在将单元格添加到其内容之前调用此方法。使用此方法来检测细胞添加,而不是监视细胞本身以查看其何时出现。

https://developer.apple.com/documentation/uikit/uicollectionviewdelegate/1618087-collectionview

© www.soinside.com 2019 - 2024. All rights reserved.