【发布时间】:2018-08-01 00:29:00
【问题描述】:
我有一个UIMenuController,在集合视图单元格顶部有一个“删除”菜单项,当用户长按第 1 节的单元格时会显示该菜单项:
@IBAction func handleLongPressOnCell(_ sender: UILongPressGestureRecognizer) {
let p = sender.location(in: collectionView)
guard sender.state == .began, let indexPath = self.collectionView.indexPathForItem(at: p), let cell = self.collectionView.cellForItem(at: indexPath) else { return }
if indexPath.section == 1 {
let frameInSuperView = collectionView.convert(cell.frame, to: view)
let deleteItem = UIMenuItem(title: "Delete", action: #selector(deleteCell))
UIMenuController.shared.menuItems = [deleteItem]
UIMenuController.shared.setTargetRect(frameInSuperView, in: view)
becomeFirstResponder()
UIMenuController.shared.setMenuVisible(true, animated: true)
}
}
如何将单元格的索引路径传递给下面的函数?我需要这些信息才能从服务器中删除对象。
@objc internal func deleteCell(sender: UIMenuItem) {
print("delete menu item tapped! print index path of selected collection view cell?")
}
【问题讨论】:
标签: ios swift uimenucontroller