ios – How to ignore taps on Accessories using modern UICollectionViewListCell + UIListContentConfiguration

0
46


I want to ignore all taps on cell’s accessory views.

The cells of my collection view are plain cells from type UICollectionViewListCell (no subclass). I am using UIListContentConfiguration and update the cell’s data within UICollectionView.CellRegistration.

My cells are also displaying UIAccessories (custom view) on the trailing edge. The accessories should only have decoration purposes (to display checkboxes).

extension UICellAccessory {
    static func checkmarkView(state: UIImage.CheckboxState) -> UICellAccessory {
        let imageView = UIImageView(image: UIImage.checkbox(state: state))
        let configuration = UICellAccessory.CustomViewConfiguration.init(
            customView: imageView,
            placement: .trailing()
        )
         // imageView.isUserInteractionEnabled = false // has no effect
        let accessory = UICellAccessory.customView(configuration: configuration)
        return accessory
    }
}

As stated in the comment setting UIImageView’s isUserInteractionEnabled somehow has no effect.

My UICollectionView looks like this:

lazy var collectionView: UICollectionView = {
   let layout = makeLayout()
   let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
   collectionView.delegate = self
   collectionView.delaysContentTouches = false
   return collectionView
}()

UICollectionView’s have isMultiSelectionEnabled disabled by default in case that matters.

When tapping the cell’s contentView it behaves as expected. It triggers the delegate’s didSelectItemAt-method like I have implemented countless times.

But when tapping the cell’s accessory view, it’s behaviour is somewhat unexpected:

Observations:

If the cell is not selected:

  • Tap on accessory invokes didHighlightItemAt. When setting a breakpoint here the cell is not visually highlighted at this point (what I would expect given the delegate-method’s name).

  • The cell does highlight the cell after leaving this method.

  • The method does not trigger didSelectItemAt.

If the cell is selected:

  • Tap on the accessory invokes didHighlightItemAt and
  • triggers didSelectItemAt after.

What I want to achieve:

A tap on a cell should:

  1. Update the datamodel (selecting the item).
  2. The cell should highlight for a short moment
  3. The checkbox should reflect the item’s selection state.