首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何知道(iOS 13+) configurationForMenuAtLocation上的上下文菜单触发了哪个configurationForMenuAtLocation单元

如何知道(iOS 13+) configurationForMenuAtLocation上的上下文菜单触发了哪个configurationForMenuAtLocation单元
EN

Stack Overflow用户
提问于 2020-04-11 14:03:44
回答 1查看 2.3K关注 0票数 6

在iOS 13上,我们有tableView & collectionView的漂亮上下文菜单。

我像这样在UICollectionView上使用它:

在“cellForItemAt索引路径”上实现:

代码语言:javascript
复制
let interaction = UIContextMenuInteraction(delegate: self)
cell.moreButton.isUserInteractionEnabled = false
cell.moreButton.tag = indexPath.row
cell.addInteraction(interaction)

在“configurationForMenuAtLocation位置”上处理老虎

代码语言:javascript
复制
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的哪个索引被触发了这个菜单?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-22 15:22:21

好的,我找到解决办法了!

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

就像这样:

代码语言:javascript
复制
@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)
    })
}

然后用这个:

代码语言:javascript
复制
@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)
}

以下是我的上下文菜单项:

代码语言:javascript
复制
let contextMenuItems = [
    ContextMenuItem(title: "Edit", image: IMAGE, index: 0),
    ContextMenuItem(title: "Remove", image: IMAGE, index: 1),
    ContextMenuItem(title: "Promote", image: IMAGE, index: 2)
]

这是我的ContextMenuItem:

代码语言:javascript
复制
struct ContextMenuItem {
  var title = ""
  var image = UIImage()
  var index = 0
}
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61158026

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档