iosuicollectionviewuikit

Is it possible to use a list-style UICollectionView but have multiple columns?


I'm working on a UICollectionView that has a custom compositional layout with multiple columns. I wanted to add swipe actions to the cells (specifically, delete).

I knew this was possible because oft he existence of trailingSwipeActionsConfigurationProvider but I didn't realize that this is only for list layouts, created with UICollectionViewCompositionalLayout.list.

But if I use this layout, I don't think I have any opportunity to add multiple columns. Do I really have to choose between multiple columns and trailing swipe actions? Or is there some way to get both?


Solution

  • Do I really have to choose between multiple columns and trailing swipe actions?

    Basically, yes, in the sense that the built-in swipe-to-delete functionality does depend, as you infer, on using a list collection layout.

    What people generally do, in other layouts, is to provide some other way of deleting. Keep in mind that selection, and in particular multiple selection, is available no matter what the layout is. Thus you can, for example, readily allow the user to go into an editing mode where they select the "cells" they want to delete, and then delete them.

    A very good example of this approach in action is the Files app. If the user elects to display the files on, say, iCloud Drive in the List layout, then the swipe-to-delete functionality is present. But if the user elects to display the files using the Icons layout, then there is a Select mode where tapping visibly selects a file (checkmark) and there is a row of actions, including Delete, at the bottom of the screen.

    enter image description here

    I suggest that you go and do likewise. You could, I suppose, in theory, provide some sort of cell-based swipe to delete that you build from scratch, but I wouldn't advise it; it isn't what users expect, and it seems like a lot of work to no purpose. But it is doable.