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

时间:2018-04-10 04:44:22

标签: ios swift uicollectionview

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

我正在写一个多图像选择器。假设限制是3张图片,在用户选择3后,所有其他图像将alpha = 0.3表示此图像无法选择。 (完全向下滚动以查看演示)

首先,这是我的代码:

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

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

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

PhotoPickerViewController:

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:

enter image description here

问题是

在我选择了3张照片之后,所有其他可见照片都有alpha = 0.3,但当我向下滚动一点时,有些照片仍有alpha = 1。我知道为什么会发生这种情况 - 因为他们不在屏幕上,所以打电话给collectionView.visibleCells不会影响他们&amp;与其他不存在的细胞不同,它们确实存在,即使它们在屏幕外。所以我想知道如何访问它们,从而使它们无法选择?

1 个答案:

答案 0 :(得分: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)
        }
    }
}