How to add accessories to a UICollectionViewListCell?
This article covers beta technology and it's up to date for Xcode 12 beta 1
In iOS 14 Apple added the ability for developers to create collection views that look and feel like table views, except they are far, far more powerful. To do this, Apple introduced a new
UICollectionViewCell subclass called
UICollectionViewListCell. This new cell class allows us to implement several tableviewcell-like principles, including accessories.
Looking for a great mobile CI/CD solution that has tons of iOS-specific tools, smooth code signing, and even real device testing? Learn more about Bitrise’s iOS specific solutions.
This sponsored message helps keep the content on this site free. Please check out this sponsor as it directly supports me and this site.
Adding accessories to a cell is done by assigning an array of
UICellAccessory items to a
accessories property. For example, to make a
UICollectionViewListCell show a disclosure indicator that makes it clear to a user that they will see more content if they tapp a cell, you would use the following code:
listCell.accessories = [.disclosureIndicator()]
You set a cell's accessories in either the
UICollectionViewDataSource method or in your CellRegistration closure.
Apple has added a whole bunch of accessories that you can add to your cells. For example
checkmark to to show the checkmark symbol that you might know from
.delete to indicate that a user can delete a cell,
.reorder to show a reorder control, and more.
You have full control over when certain accessories are displayed and their color. For example, a reorder control is normally only visible when a collection view is in edit mode. To make it always visible, and make it orange instead of gray you'd use the following code:
cell.accessories = [.reorder(displayed: .always, options: .init(tintColor: .orange, showsVerticalSeparator: false))]
Every accessory has its own options object with different parameters. Refer to the documentation to see the configuration options for accessories you're interested in.