So I have a main object that has many images associated with it. An Image is also an object.
Say you have a collection view controller, and in that controller you have
cellForItemAtIndexPath
well based on the main object, if it has the current image associated with it I want to set selected to true. But I want the user to be able to "un-select" that current cell at any time to remove its association with the main object.
I find that if you set "selected to true" - if there is an relation between the main object and image in cellForItemAtIndexPath
, de-selection is no longer an option.
in
didDeselectItemAtIndexPath
and
didSelectItemAtIndexPath
I test with a log to see if they are called. If a cell is set to selected - nether gets called, but If I never set a cell to selected in cellForItemAtIndexPath
I can select and deselect all I want.
Is this the intended way a collection view is supposed to work? I read the docs and it does not seem to talk about this being so. I interpret the docs to mean it works the way a table view cell would. with a few obvious changes
This also shows the controller is set up correct and is using the appropriate delegate methods.... hmmmm
Do you have a custom
setSelected
method in your Cell class? Are you calling[super setSelected:selected]
in that method?I had a mysterious problem where, I was using multiple selection, I could not deselect cells once they were selected. Calling the super method fixed the problem.
I had the same issue, ie. setting
cell.selected = YES
in[UICollectionView collectionView:cellForItemAtIndexPath:]
then not being able to deselect the cell by tapping on it.Solution for now: I call both
[UICollectionViewCell setSelected:]
and[UICollectionView selectItemAtIndexPath:animated:scrollPosition:]
in[UICollectionView collectionView:cellForItemAtIndexPath:]
.This is kind of old but, since I encounter the same issue using swift I will add my answer. When using:
The cell didn't get selected at all. But when using:
It did get selected but I wasn't able to select/deselect the cell anymore.
My solution (use both methods):
When this two methods are called in:
It worked perfectly!
Simple Solution i found
Living in the age of iOS 9, there are multiple things to check here.
collectionView.allowsSelection
set toYES
collectionView.allowsMultipleSelection
set toYES
(if you need that ability)Now comes the fan part. If you listen to Apple and set
backgroundColor
on thecell.contentView
instead ofcell
itself, then you have just hidden itsselectedBackgroundView
from ever being visible. Because:So if you want to use selectedBackgroundView (which is the one being turned on/off with
cell.selected
andselectItemAtIndexPath...
) then do this:and it should work just fine.
Cell selection and deselection is best handled by setting a backgroundView and a selected background view. I recommend making sure that both of these views' frames are set correctly in the layoutSubviews method (if you set the selected and background view via IB).
Don't forget to set your contentView's (if you have one) background color to clear so the correct background view shows through.
Never set the cell's selection directly (i.e. via cell.selected = YES), use the methods designed for this purpose in the collection view. It is clearly explained in the documents, although I will agree the information is somewhat fragmented across guides.
You should not need to poke into a cell's background colors directly in your collectionView datasource.
Also, as a final note, don't forget to call [super prepareForReuse] and [super setSelected:selected] if you are implementing these in your cell's class, as you might be preventing the cell's superclass from doing the cell selection.
Hit me up if you need further clarification on this subject.