UICollectionView shouldShowMenuForItemAt 未调用 [英] UICollectionView shouldShowMenuForItemAt Not Called
问题描述
我在作为其委托的 UIViewController
中有一个标准的 UICollectionView
.但是 shouldShowMenuForItemAt
函数不会被长按调用.我添加了一个 didSelectItemAt
函数,它在单击单元格时会被调用,以确保委托确实连接正确.
I have a stock standard UICollectionView
in a UIViewController
that is its delegate. However the shouldShowMenuForItemAt
func is not called for a long press. I have added a didSelectItemAt
func which does get called on clicking a cell to make sure the delegate is indeed wired up correctly.
我还实现了 canPerformAction
以在委托中返回 true 和 performAction
以及 canPerformAction
和 canBecomeFirstResponder
在我的 UICollectionViewCell
子类中返回 true.长按单元格时不会调用这些 func 中的任何一个.有什么建议吗?
I also implemented the canPerformAction
to return true and performAction
in the delegate along with the canPerformAction
and canBecomeFirstResponder
to return true in my UICollectionViewCell
subclass. None of these func's get called for a long press of a cell. Any suggestions?
推荐答案
大多数人似乎都忽略的难题是,为了使菜单工作(在集合视图或表格视图中),cell 必须实现选择器.
The missing piece of the puzzle, which most people seem to miss, is that in order for menus to work (in a collection view or table view), the cell must implement the selector.
这是一个最小的例子.说明:使用 Single View App 模板创建一个新项目.复制此代码并将其粘贴到 ViewController.swift 中,以便完全替换该文件中的所有内容.跑.长按绿色方块.享受.(菜单项什么都不做;关键是,您将看到菜单项出现.)
Here's a minimal example. Instruction: Make a new project using the Single View App template. Copy this code and paste it into ViewController.swift, so as to replace completely everything in that file. Run. Long press on a green square. Enjoy. (The menu item does nothing; the point is, you will see the menu item appear.)
import UIKit
class Cell : UICollectionViewCell {
@objc func f(_ : Any) {}
}
class ViewController: UIViewController {
let cellid = "cellid"
@nonobjc private let howdy = #selector(Cell.f)
override func viewDidLoad() {
super.viewDidLoad()
let cv = UICollectionView(frame: self.view.bounds, collectionViewLayout: UICollectionViewFlowLayout())
self.view.addSubview(cv)
cv.autoresizingMask = [.flexibleWidth, .flexibleHeight]
cv.delegate = self
cv.dataSource = self
cv.register(Cell.self, forCellWithReuseIdentifier: cellid)
}
}
extension ViewController : UICollectionViewDataSource, UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 100
}
func collectionView(_ cv: UICollectionView, cellForItemAt ip: IndexPath) -> UICollectionViewCell {
let cell = cv.dequeueReusableCell(withReuseIdentifier: cellid, for: ip)
cell.backgroundColor = .green
return cell
}
func collectionView(_ collectionView: UICollectionView, shouldShowMenuForItemAt indexPath: IndexPath) -> Bool {
let mi = UIMenuItem(title:"Howdy", action:howdy)
UIMenuController.shared.menuItems = [mi]
return true
}
func collectionView(_ collectionView: UICollectionView, canPerformAction action: Selector, forItemAt indexPath: IndexPath, withSender sender: Any?) -> Bool {
return (action == howdy)
}
func collectionView(_ collectionView: UICollectionView, performAction action: Selector, forItemAt indexPath: IndexPath, withSender sender: Any?) {
}
}
这篇关于UICollectionView shouldShowMenuForItemAt 未调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!