UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

How do I implement selection, deselection, and deselection on second tap in UICollectionView?

Forums > Swift

Hi everyone,

I am wondering if there is a definitive method/best practice for implementing the selection and deselection of items in a UICollectionView. I have tried many different solutions, but none achieve the intended effect.

The collection view is a bar graph, similar to the hourly weather bar graph in Carrot Weather, or the daily step bar graph in Pedometer++. The behavior I want to achieve is almost identical to the selection and deselection of cells in Carrot Weather.

Here is what I want to achieve:

  • Tap a cell in collection view to change the color of items in that cell (i.e. .backgroundColor = .red)
  • Update other labels according to the data that corresponds to the cell at that selected indexPath. For instance, if it was a weather app, tapping on a cell for a specific day would change the app's central label to display the daily temperature that corresponds to that cell
  • That same cell should stay selected regardless of scrolling the collection view
  • Tapping a different cell resets the first tapped cell to its original state (i.e. previously selected cell's .backgroundColor = .clear, newly selected cell's .backgroundColor = .red, temperature corresponds to newly selected cell)
  • Tapping the same cell, which is already selected, deselects the cell and turns it back to its original state. Labels are updated to display default information (i.e., .backgroundColor = .clear, temperature corresponds to today's temperature)

I have managed to achieve some of these features at various times, but never all at once. It seems like there would be a straightforward method for selecting and deselecting items, but all of the methods I have found or come up with seem hacky (in a bad way) and have lead to problems.

If anyone has any advice, I would be extremely grateful!

Thank you, -Thomas

3      

Hi Thomas,

you can either implement the select/deselect methods in the delegate and then override the isSelected in your cell that will change its appearance.

Another approach (which I prefer) is to save the selected state in your model which will then influence the cell appearance when setting in the cellForRowAt. If you also implement prepareForReuse and reset the appearance there this will make sure the wrong cell never appear as selected.

Nowadays I would implement this with the diffable data source and use the snapshot to reload selections when user taps the cell.

4      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.