swift - 如何知道在(iOS 13+)configurationForMenuAtLocation 上为上下文菜单触发了哪个 UICollectionView 单元格

标签 swift uicollectionview contextmenu ios13

在 iOS 13 上,我们为 tableView 和 collectionView 提供了漂亮的上下文菜单。

我在 UICollectionView 上使用它,如下所示:

'cellForItemAt indexPath' 上的实现:

let interaction = UIContextMenuInteraction(delegate: self)
cell.moreButton.isUserInteractionEnabled = false
cell.moreButton.tag = indexPath.row
cell.addInteraction(interaction)

在“configurationForMenuAtLocation 位置”上处理老虎
func contextMenuInteraction(_ interaction: UIContextMenuInteraction, configurationForMenuAtLocation location: CGPoint) -> UIContextMenuConfiguration? {
        let configuration = UIContextMenuConfiguration(identifier: nil, previewProvider: nil) { suggestedActions in
            var actions = [UIAction]()
            for item in self.contextMenuItems {
                let action = UIAction(title: item.title, image: item.image, identifier: nil, discoverabilityTitle: nil) { _ in
                self.didSelectContextMenu(index: 0) <== how pass the index from here? 
          }
         actions.append(action)
       }
      let cancel = UIAction(title: "Cancel", attributes: .destructive) { _ in}
      actions.append(cancel)
      return UIMenu(title: "", children: actions)
    }
  return configuration
}

问题是我应该如何知道这个菜单触发了 collectionView 的哪个索引?

最佳答案

好的,我找到了解决方案!

我应该使用“contextMenuConfigurationForItemAt”而不是“configurationForMenuAtLocation”。

像这样:

@available(iOS 13.0, *)
func collectionView(_ collectionView: UICollectionView, contextMenuConfigurationForItemAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? {
    return UIContextMenuConfiguration(identifier: nil, previewProvider: nil, actionProvider: { suggestedActions in
        return self.makeContextMenu(for: indexPath.row)
    })
}

然后使用这个:
@available(iOS 13.0, *)
func makeContextMenu(for index:Int) -> UIMenu {
    var actions = [UIAction]()
    for item in self.contextMenuItems {
        let action = UIAction(title: item.title, image: item.image, identifier: nil, discoverabilityTitle: nil) { _ in
            self.didSelectContextMenu(menuIndex: item.index, cellIndex: index)  // Here I have both cell index & context menu item index
        }
        actions.append(action)
    }
    let cancel = UIAction(title: "Cancel", attributes: .destructive) { _ in}
    actions.append(cancel)
    return UIMenu(title: "", children: actions)
}

这是我的上下文菜单项:
let contextMenuItems = [
    ContextMenuItem(title: "Edit", image: IMAGE, index: 0),
    ContextMenuItem(title: "Remove", image: IMAGE, index: 1),
    ContextMenuItem(title: "Promote", image: IMAGE, index: 2)
]

这是我的 ContextMenuItem:
struct ContextMenuItem {
  var title = ""
  var image = UIImage()
  var index = 0
}

关于swift - 如何知道在(iOS 13+)configurationForMenuAtLocation 上为上下文菜单触发了哪个 UICollectionView 单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61158026/

相关文章:

swift - 使用闭包设置属性报错

ios - 使用 NotificationCenter 和 Observer 在 UIViewControllers 之间发送 bool 值

ios - 在 iphone X 中带有粘性页脚的 UICollectionView,内容滚动到页脚下方的区域

c# - 为什么在 4 月份从一体化代码框架中删除了所有 C# Shell 扩展示例?

JavaFX ChoiceBox 上下文菜单位置

swift - Xcode 创建了一个返回错误 "missing setter or instance variable"的 Xib 文件

ios - 使用通用协议(protocol)实例化 ViewModel 数据

ios - Swift iOS - 如何找到 CollectionView 第二部分的 CGPoint x/y 值

ios - 如何使用 NSFetchedResultsController 和多个获取的实体设置 collectionView(或 TableView)

javascript - 将点击的元素传递给 Firefox 中的上下文菜单操作