Ios UIMenuItem未显示在表中
我正在尝试将自定义操作添加到Ios UIMenuItem未显示在表中,ios,swift,uikit,Ios,Swift,Uikit,我正在尝试将自定义操作添加到UIMenuController,以便在UITableViewCell上使用,但在显示菜单时它不会出现 编辑:修订代码 代码如下: class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { override func viewDidLoad() { ... UIMenuController.shared.menuIt
UIMenuController
,以便在UITableViewCell
上使用,但在显示菜单时它不会出现
编辑:修订代码
代码如下:
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
override func viewDidLoad() {
...
UIMenuController.shared.menuItems = [UIMenuItem(title: "Test", action: #selector(test))]
UIMenuController.shared.update()
}
// Table view setup
// ...
func tableView(_ tableView: UITableView, shouldShowMenuForRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, canPerformAction action: Selector, forRowAt indexPath: IndexPath, withSender sender: Any?) -> Bool {
return action == #selector(copy(_:)) || action == #selector(test)
}
func tableView(_ tableView: UITableView, performAction action: Selector, forRowAt indexPath: IndexPath, withSender sender: Any?) {
}
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
return action == #selector(test)
}
@objc func test() {
print("Hello, world!")
}
}
test
函数需要位于UITableViewCell
子类中UITableViewCell
子类中实现canperformation(操作:选择器,带有sender-sender:AnyObject?->Bool
,并返回return action==#选择器(测试)
UIMenuController.shared.menuItems=[UIMenuItem(标题:“测试”,操作:#选择器(测试))]
将#选择器(测试)
更改为#选择器(YourCellSubclass.Test)
| action==#选择器(test)
更改为| action=#选择器(YourCellSubclass.test)
class ViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
UIMenuController.shared.menuItems = [UIMenuItem(title: "Test", action: #selector(MyCell.test))]
UIMenuController.shared.update()
tableView.register(MyCell.self, forCellReuseIdentifier: "my")
// Do any additional setup after loading the view, typically from a nib.
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "my", for: indexPath) as! MyCell
cell.textLabel?.text = "\(indexPath.row)"
return cell
}
override func tableView(_ tableView: UITableView, shouldShowMenuForRowAt indexPath: IndexPath) -> Bool {
return true
}
override func tableView(_ tableView: UITableView, canPerformAction action: Selector, forRowAt indexPath: IndexPath, withSender sender: Any?) -> Bool {
return action == #selector(MyCell.test)
}
override func tableView(_ tableView: UITableView, performAction action: Selector, forRowAt indexPath: IndexPath, withSender sender: Any?) {
// needs to be here
}
}
单元格:
UIMenuController.menuVisible=true myMenuController.arrowDirection=UIMenuControllerArrowDirection.Down//set rect、view myMenuController.setTargetRect(CGRectZero,inView:self.view)不做任何事情的你试试这个链接,它还是不起作用。请参阅我的编辑,了解我修改过的代码。@LakhanMankani我已在答案中添加了工作示例。
class MyCell: UITableViewCell {
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
return action == #selector(test)
}
@objc func test() {
print("works")
}
}