Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UICollectionView Select and Deselect issue

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

like image 715
bworby Avatar asked Mar 11 '13 03:03

bworby


People also ask

What is the difference between Uitableview and UICollectionView?

UICollectionView is the model for displaying multidimensional data — it was introduced just a year or two ago with iOS 6, I believe. The main difference between the two is how you want to display 1000 items. UITableViewhas a couple of styles of cells stacked one over the other.

How do I select an item in CollectionView?

Single selectionWhen the SelectionMode property is set to Single , a single item in the CollectionView can be selected. When an item is selected, the SelectedItem property will be set to the value of the selected item.

How do I remove cells from CollectionView?

Build and Run the project and select the Edit Button. Select a few cells and press the Trash button to remove the items.

How does swift implement UICollectionView?

Select the Main storyboard from the file browser. Add a CollectionView by pressing command shift L to open the storyboard widget window. Drag the collectionView onto the main view controller. Add constraints to the UICollectionView widget to ensure that the widget fills the screen on all devices.


2 Answers

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:].

like image 154
user2405793 Avatar answered Sep 18 '22 22:09

user2405793


I had a Deselect issue with UICollectionView and I found that I was not allowing multiple selection on collectionView. So when I was testing I tried always on the same cell and if single selection is ON you can't Deselect a cell already selected.

I had to add:

myCollectionView.allowsMultipleSelection = YES; 
like image 44
Fjohn Avatar answered Sep 21 '22 22:09

Fjohn