jamiii
jamiii

Reputation: 95

Cannot deselect pre-selected uicollectionviewcells on allowsMultipleSelection

I’m having some problems with a collectionview and the multi selection. The problem comes when I’m pre-selecting it, then I cannot manage to deselect it. I've made a sample project in order to someone can check this out. I've uploaded to my github profile: https://github.com/ahhhv/CollectionViewSelection

But I'll put some code in here in case somebody knows already the answer.

// Cell
override public var isSelected: Bool {
    didSet {
        if isSelected {
            layer.borderWidth = 1
            layer.borderColor = UIColor.systemBlue.cgColor
            titleLabel.textColor = .systemBlue
        } else {
            layer.borderWidth = 0
            titleLabel.textColor = .black
        }

        titleLabel.accessibilityLabel = self.isSelected ? "\(selectedItemText) \(isSelectedText)" : selectedItemText
        titleLabel.accessibilityTraits = UIAccessibilityTraits.button
    }
}

// VC
    public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    guard let item = item else { return UICollectionViewCell() }

    if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ModuleSelectorCell.reuseID, for: indexPath) as? ModuleSelectorCell {
        cell.set(item: item.items[indexPath.item])

        if item.selectionType == .exclusive {
            if let selectedItem = item.selectedItems, let first = selectedItem.first {
                if first == indexPath.item {
                    cell.isSelected = true
                }
            }
        } else {
            if let selectedItems = item.selectedItems {
                selectedItems.forEach { num in
                    if num == indexPath.item {
                        cell.isSelected = true
                    }
                }
            }
        }
        
        return cell
    }

    return UICollectionViewCell()
}

I know it's a long shot but I've been struggling with this for quite some time.

Kindest regards,

Upvotes: 3

Views: 446

Answers (1)

Sauvik Dolui
Sauvik Dolui

Reputation: 5660

Any specific reason behind setting isSelected inside collectionView(_:cellForItemAt:)? Apple Documentation DOES NOT SUGGEST TO DO SO.

Discussion

This property manages the selection state of the cell only. The default value of this property is false, which indicates an unselected state.

You typically do not set the value of this property directly. Changing the value of this property programmatically does not change the appearance of the cell. The preferred way to select the cell and highlight it is to use the selection methods of the collection view object.

So my suggestion will be to do the following modification to fix your issue.

enter image description here

The only problem you need to deal with is that - you can not avoid setting scrollPosition in selectItem(at:animated:scrollPosition). I guess if all pre-selected items are at top of your collection view, then it won't be a be a problem to pass .top for it's value. Otherwise you need to come up with a better strategy. Anyway, you can always scroll to the top using

collectionView.setContentOffset(.zero, animated: false)

to position your collectionView's contentOffset.

Upvotes: 1

Related Questions