I'm trying to preselect a cell in cellForItem(at index: Int) -> UICollectionViewCell by cell.isSelected = true in section controller.
It works fine and cell becomes selected. When I try to deselect a cell by tapping it cell doesn't react at all. Tap triggers isHighlighted method but doesn't want to trigger neither isSelected in cell nor didDeselectItemAt delegate method.
This issue is related only to preselected cells. All other cells work fine in the same collection.
@pharosproduction can you describe a bit what you're trying to do? UICollectionViewCell selection APIs aren't really supposed to be changed manually, you're supposed to let the UICollectionView internals handle all of that for you.
The docs say:
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.
If you're trying to do custom selection state, we recommend making your own properties and settings.
There is a Set of posts displayed as a two column list, one section with one row for each post. Some of them should be selected when list appears. Each post contains a selection state(bool). Posts amount is a variable(pagination load). What is the best way to select marked for selection cells and where?
I have tried already different approaches including selectItem(at: IndexPath?, animated: Bool, scrollPosition: UICollectionViewScrollPosition) but no luck to find the best way to do this. The goal is just to select a cell without triggering any delegate methods so user can deselect it or proceed with a default selection set(fetched by indexPathForSelectedItems)
@pharosproduction at Instagram we use custom properties to represent selected state in our UI instead of relying on the UICollectionView built-in stuff. We would do something like:
@property BOOL checkmarkDisplayed; to our cell subclasssetCheckmarkDisplayed:(BOOL)checkmarkDisplayed setter, set the state and call [self setNeedsLayout] and set hidden on our checkmark image viewlayoutSubviewscellForItemAtIndex: we can manually set the state w/out worrying about UICollectionView internals.Then in didSelectItemAtIndex: you can flip the selection state, both in the cell and wherever you're tracking it.
Got it. Thanks.
BTW, collectionView doesn't trigger didSelectItemAtIndex:, only deselect. Is it possible to switch from didSelectItem(at index: Int) in section to collectionView delegate without loosing all other IGListSectionController functionality?
@pharosproduction you mean to use the UICollectionViewDelegate APIs alongside IGListSectionController? It's possible, but not at the section-controller level. You can attach a collectionViewDelegate to the adapter to get all the normal delegate events.
No, on ViewController level. didDeselectItemAtIndex works fine, but didSelectItemAtIndex doesn't work, instead IGListKit triggers didSelectItem at section level. Is it possible to trigger both of them(didSelectItemAtIndex and didSelectItem)?
Actually I understand it's possible by digging inside IGListKit and changing current code, but maybe I'm wrong and I have missed something in docs.
@pharosproduction it should be totally possible. That collectionViewDelegate should just forward events to the delegate while also notifying the section controller. Here's the source.
I have updated IGListKit to the latest master. It works, thank you.
馃挴
Most helpful comment
@pharosproduction at Instagram we use custom properties to represent selected state in our UI instead of relying on the
UICollectionViewbuilt-in stuff. We would do something like:@property BOOL checkmarkDisplayed;to our cell subclasssetCheckmarkDisplayed:(BOOL)checkmarkDisplayedsetter, set the state and call[self setNeedsLayout]and sethiddenon our checkmark image viewlayoutSubviewscellForItemAtIndex:we can manually set the state w/out worrying aboutUICollectionViewinternals.Then in
didSelectItemAtIndex:you can flip the selection state, both in the cell and wherever you're tracking it.