Wrong UIMenu for cell after collection view reload

111 Views Asked by At

I'm having issues with a UIMenu attached to the UIButton for a cell. So the UIMenu on a button (button is part of a cell) in a collection view, and if while UIMenu is presented there’s a collectionView.reloadData() then UIMenu changes position AND is transferred to another cell, using new cell data for actions. So it seems that after reloadData(), UIMenu is using the cell/data not of the primarily selected cell.

Here’s the code I’m using: https://pastebin.com/wuNsPki5

How to test what issue I have:

  1. Run app
  2. Tap on any cell
  3. Wait 10 seconds
  4. Tap share action
  5. Output says it shared another value than it was expected
UIAction(title: "Share") { [weak self] _ in
    guard let self else { return }
    print("Share \(self.value)")
}

So if I tapped on second cell (value == 1), wait 10 seconds (trigger collectionView.reloadData) and then tap Share Action, I'd expect that it will print "Share 1", but it actually prints "Share 3" for example.

1

There are 1 best solutions below

1
Smartcat On

Two separate ideas:

  1. Simply don't reloadData() while the menu is up. I mean, that is pulling the rug out from under the menu and the button it's attached to, so to speak.

  2. When the user taps the button in the UICollectionView cell, have it trigger an invisible button's UIMenu that lives outside of the collection view, atop it. Then the collection view can reload its data all it wants while the menu is up. Now if cells are added or removed, the tail of the UIMenu may not be in the right place anymore, but you wouldn't want to move the UIMenu around while it's being shown anyways as that'd be trouble for the user.

I've never tried either of these approaches, but it's what I'd explore if in this situation.