更新 UIMenuController 中的菜单项
Updating Menu Items In UIMenuController
我想更新 tableview 菜单项控制器中的菜单项,因为现在我只得到这些
我已经实现了这个:
func tableView(_ tableView: UITableView, shouldShowMenuForRowAt indexPath: IndexPath) -> Bool {
let forword = UIMenuItem(title: "Demo", action: #selector(self.demo))
UIMenuController.shared.menuItems?.append(forword)
UIMenuController.shared.update()
return true
}
func tableView(_ tableView: UITableView, performAction action: Selector, forRowAt indexPath: IndexPath, withSender sender: Any?) {
UIMenuController.shared.setMenuVisible(true, animated: true)
}
func tableView(_ tableView: UITableView, canPerformAction action: Selector, forRowAt indexPath: IndexPath, withSender sender: Any?) -> Bool {
return true
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.clearAllSelectedCell()
}
但我的要求是:
我怎样才能做到这一点?
您的代码有一些问题……
- 不要使用
UIMenuController.shared.menuItems?.append
,直接设置menuItems
- 不要在
shouldShowMenuForRowAt
中这样做,只在 viewDidLoad
中这样做一次
- 你不需要
setMenuVisible
最重要的是
- 菜单项的操作必须是 table 视图单元格子类上的方法。
所以,在你的情况下……
你的 table 视图控制器应该有这些方法…
override func viewDidLoad() {
super.viewDidLoad()
let forward = UIMenuItem(title: "Forward", action: #selector(MyCell.menuItemTapped(_ :)))
let delete = UIMenuItem(title: "Delete", action: #selector(MyCell.menuItemTapped(_ :)))
UIMenuController.shared.menuItems = [forward, delete]
UIMenuController.shared.update()
}
override func tableView(_ tableView: UITableView, shouldShowMenuForRowAt indexPath: IndexPath) -> Bool {
return true
}
override func tableView(_ tableView: UITableView, performAction action: Selector, forRowAt indexPath: IndexPath, withSender sender: Any?) {
}
override func tableView(_ tableView: UITableView, canPerformAction action: Selector, forRowAt indexPath: IndexPath, withSender sender: Any?) -> Bool {
return true
}
你的手机应该有……
class MyCell: UITableViewCell {
@objc func menuItemTapped(_ sender: UIMenuController) {
}
}
1. create protocol to provide control into your view controller :
public protocol MenuItemDelegate {
func copyAction(cell: UITableViewCell)
func forwordAction(cell: UITableViewCell)
func deleteAction(cell: UITableViewCell)
}
2. create a custom table cell:
class MyTableCell: UITableViewCell {
var menuItemDelegate: MenuItemDelegate!
var isCopyEnable = true
var isForwardEnable = true
var isDeleteEnable = true
override func awakeFromNib() {
super.awakeFromNib()
}
func setUpmenu(){
let menu = UIMenuController.shared
let forword = UIMenuItem(title: "Forward", action: #selector(self.forword(_:)))
let delete = UIMenuItem(title: "Delete", action: #selector(self.deleteAction(_:)))
menu.menuItems = [forword,delete]
if !isDeleteEnable{
menu.menuItems?.remove(at: (menu.menuItems?.index(of: delete))!)
}
if !isForwardEnable{
menu.menuItems?.remove(at: (menu.menuItems?.index(of: forword))!)
}
menu.update()
}
override public func copy(_ sender: Any?) {
UIPasteboard.general.string = accessibilityValue
menuItemDelegate.copyAction(cell: self)
UIMenuController.shared.setMenuVisible(false, animated: true)
}
@objc public func forword(_ sender: Any?) {
menuItemDelegate.forwordAction(cell: self)
UIMenuController.shared.setMenuVisible(false, animated: true)
}
@objc public func deleteAction(_ sender: Any?) {
menuItemDelegate.deleteAction(cell: self)
UIMenuController.shared.setMenuVisible(false, animated: true)
}
override public func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
return (action == #selector(copy(_:)) || action == #selector(forword(_:)) || action == #selector(deleteAction(_:)))
}
}
3. Implement in view controller as like :
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MyTableCell", for: indexPath) as! MyTableCell
cell.isForwardEnable = false
cell.setUpmenu()
return cell
}
func tableView(_ tableView: UITableView, shouldShowMenuForRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, performAction action: Selector, forRowAt indexPath: IndexPath, withSender sender: Any?) {
UIMenuController.shared.setMenuVisible(true, animated: true)
}
func tableView(_ tableView: UITableView, canPerformAction action: Selector, forRowAt indexPath: IndexPath, withSender sender: Any?) -> Bool {
return true
}
它将帮助您实现这一目标。
按照以下步骤,实现长按自定义物品。
将下面的代码粘贴到 viewDidload 方法中
let reply = UIMenuItem(title: "Reply", action: #selector(MessageCollectionViewCell.reply(_ :)))
让编辑 = UIMenuItem(标题:"Edit",操作:#selector(MessageCollectionViewCell.edit(_ :)))
let forward = UIMenuItem(title: "Forward", action: #selector(MessageCollectionViewCell.forward(_ :)))
UIMenuController.shared.menuItems = [回复、编辑、转发]
UIMenuController.shared.update()
自定义单元格的扩展
扩展 MessageCollectionViewCell {
@objc func reply(_ sender: Any?) {
// 获取集合视图
如果让 collectionView = self.superview 作为? UICollectionView {
// 获取索引路径
如果让 indexPath = collectionView.indexPath(for: self) {
// 触发动作
collectionView.delegate?.collectionView?(collectionView, performAction: #selector(MessageCollectionViewCell.reply(_:)), forItemAt: indexPath, withSender: 发件人)
}
}
}
@objc func edit(_ sender: Any?) {
// 获取集合视图
如果让 collectionView = self.superview 作为? UICollectionView {
// 获取索引路径
如果让 indexPath = collectionView.indexPath(for: self) {
// 触发动作
collectionView.delegate?.collectionView?(collectionView, performAction: #selector(MessageCollectionViewCell.edit(_:)), forItemAt: indexPath, withSender: 发件人)
}
}
}
@objc func forward(_ sender: Any?) {
// 获取集合视图
如果让 collectionView = self.superview 作为? UICollectionView {
// 获取索引路径
如果让 indexPath = collectionView.indexPath(for: self) {
// 触发动作
collectionView.delegate?.collectionView?(collectionView, performAction: #selector(MessageCollectionViewCell.forward(_:)), forItemAt: indexPath, withSender: 发件人)
}
}
}
}
在您的控制器中添加此代码,在我的例子中这样做 (class ChatViewController: MessagesViewController{})
// -------------------------------------- ------------------------------------------
// MARK: - 处理长按动作
// ---------------------------------------------- --------------------------------------
override func collectionView(_ collectionView: UICollectionView, shouldShowMenuForItemAt indexPath: IndexPath) -> Bool {
return 真
}
override func collectionView(_ collectionView: UICollectionView, canPerformAction action: Selector, forItemAt indexPath: IndexPath, withSender sender: Any?) -> Bool {
打印(action.description)
让消息 = 消息[indexPath.section]
//let message = messagesDataSource.messageForItem(at: indexPath, in: messagesCollectionView)
if action == NSSelectorFromString("reply:") {
return true
}
else if action == NSSelectorFromString("edit:") {
return true
}
else if action == NSSelectorFromString("forward:") {
return true
}
else {
return super.collectionView(collectionView, canPerformAction: action, forItemAt: indexPath, withSender: sender)
}
}
override func collectionView(_ collectionView: UICollectionView, performAction action: Selector, forItemAt indexPath: IndexPath, withSender sender: Any?) {
if action == NSSelectorFromString("reply:") {
print("reply item here!!")
}
else if action == NSSelectorFromString("edit:") {
print("edit item here!!")
}
else if action == NSSelectorFromString("forward:") {
print("forward item here!!")
}
else {
super.collectionView(collectionView, performAction: action, forItemAt: indexPath, withSender: sender)
}
}
我想更新 tableview 菜单项控制器中的菜单项,因为现在我只得到这些
我已经实现了这个:
func tableView(_ tableView: UITableView, shouldShowMenuForRowAt indexPath: IndexPath) -> Bool {
let forword = UIMenuItem(title: "Demo", action: #selector(self.demo))
UIMenuController.shared.menuItems?.append(forword)
UIMenuController.shared.update()
return true
}
func tableView(_ tableView: UITableView, performAction action: Selector, forRowAt indexPath: IndexPath, withSender sender: Any?) {
UIMenuController.shared.setMenuVisible(true, animated: true)
}
func tableView(_ tableView: UITableView, canPerformAction action: Selector, forRowAt indexPath: IndexPath, withSender sender: Any?) -> Bool {
return true
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.clearAllSelectedCell()
}
但我的要求是:
我怎样才能做到这一点?
您的代码有一些问题……
- 不要使用
UIMenuController.shared.menuItems?.append
,直接设置menuItems
- 不要在
shouldShowMenuForRowAt
中这样做,只在viewDidLoad
中这样做一次
- 你不需要
setMenuVisible
最重要的是
- 菜单项的操作必须是 table 视图单元格子类上的方法。
所以,在你的情况下……
你的 table 视图控制器应该有这些方法…
override func viewDidLoad() {
super.viewDidLoad()
let forward = UIMenuItem(title: "Forward", action: #selector(MyCell.menuItemTapped(_ :)))
let delete = UIMenuItem(title: "Delete", action: #selector(MyCell.menuItemTapped(_ :)))
UIMenuController.shared.menuItems = [forward, delete]
UIMenuController.shared.update()
}
override func tableView(_ tableView: UITableView, shouldShowMenuForRowAt indexPath: IndexPath) -> Bool {
return true
}
override func tableView(_ tableView: UITableView, performAction action: Selector, forRowAt indexPath: IndexPath, withSender sender: Any?) {
}
override func tableView(_ tableView: UITableView, canPerformAction action: Selector, forRowAt indexPath: IndexPath, withSender sender: Any?) -> Bool {
return true
}
你的手机应该有……
class MyCell: UITableViewCell {
@objc func menuItemTapped(_ sender: UIMenuController) {
}
}
1. create protocol to provide control into your view controller :
public protocol MenuItemDelegate {
func copyAction(cell: UITableViewCell)
func forwordAction(cell: UITableViewCell)
func deleteAction(cell: UITableViewCell)
}
2. create a custom table cell:
class MyTableCell: UITableViewCell {
var menuItemDelegate: MenuItemDelegate!
var isCopyEnable = true
var isForwardEnable = true
var isDeleteEnable = true
override func awakeFromNib() {
super.awakeFromNib()
}
func setUpmenu(){
let menu = UIMenuController.shared
let forword = UIMenuItem(title: "Forward", action: #selector(self.forword(_:)))
let delete = UIMenuItem(title: "Delete", action: #selector(self.deleteAction(_:)))
menu.menuItems = [forword,delete]
if !isDeleteEnable{
menu.menuItems?.remove(at: (menu.menuItems?.index(of: delete))!)
}
if !isForwardEnable{
menu.menuItems?.remove(at: (menu.menuItems?.index(of: forword))!)
}
menu.update()
}
override public func copy(_ sender: Any?) {
UIPasteboard.general.string = accessibilityValue
menuItemDelegate.copyAction(cell: self)
UIMenuController.shared.setMenuVisible(false, animated: true)
}
@objc public func forword(_ sender: Any?) {
menuItemDelegate.forwordAction(cell: self)
UIMenuController.shared.setMenuVisible(false, animated: true)
}
@objc public func deleteAction(_ sender: Any?) {
menuItemDelegate.deleteAction(cell: self)
UIMenuController.shared.setMenuVisible(false, animated: true)
}
override public func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
return (action == #selector(copy(_:)) || action == #selector(forword(_:)) || action == #selector(deleteAction(_:)))
}
}
3. Implement in view controller as like :
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MyTableCell", for: indexPath) as! MyTableCell
cell.isForwardEnable = false
cell.setUpmenu()
return cell
}
func tableView(_ tableView: UITableView, shouldShowMenuForRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, performAction action: Selector, forRowAt indexPath: IndexPath, withSender sender: Any?) {
UIMenuController.shared.setMenuVisible(true, animated: true)
}
func tableView(_ tableView: UITableView, canPerformAction action: Selector, forRowAt indexPath: IndexPath, withSender sender: Any?) -> Bool {
return true
}
它将帮助您实现这一目标。
按照以下步骤,实现长按自定义物品。
将下面的代码粘贴到 viewDidload 方法中 let reply = UIMenuItem(title: "Reply", action: #selector(MessageCollectionViewCell.reply(_ :))) 让编辑 = UIMenuItem(标题:"Edit",操作:#selector(MessageCollectionViewCell.edit(_ :))) let forward = UIMenuItem(title: "Forward", action: #selector(MessageCollectionViewCell.forward(_ :))) UIMenuController.shared.menuItems = [回复、编辑、转发] UIMenuController.shared.update()
自定义单元格的扩展 扩展 MessageCollectionViewCell {
@objc func reply(_ sender: Any?) { // 获取集合视图 如果让 collectionView = self.superview 作为? UICollectionView { // 获取索引路径 如果让 indexPath = collectionView.indexPath(for: self) { // 触发动作 collectionView.delegate?.collectionView?(collectionView, performAction: #selector(MessageCollectionViewCell.reply(_:)), forItemAt: indexPath, withSender: 发件人) } } }
@objc func edit(_ sender: Any?) { // 获取集合视图 如果让 collectionView = self.superview 作为? UICollectionView { // 获取索引路径 如果让 indexPath = collectionView.indexPath(for: self) { // 触发动作 collectionView.delegate?.collectionView?(collectionView, performAction: #selector(MessageCollectionViewCell.edit(_:)), forItemAt: indexPath, withSender: 发件人) } } }
@objc func forward(_ sender: Any?) { // 获取集合视图 如果让 collectionView = self.superview 作为? UICollectionView { // 获取索引路径 如果让 indexPath = collectionView.indexPath(for: self) { // 触发动作 collectionView.delegate?.collectionView?(collectionView, performAction: #selector(MessageCollectionViewCell.forward(_:)), forItemAt: indexPath, withSender: 发件人) } } }
}
在您的控制器中添加此代码,在我的例子中这样做 (class ChatViewController: MessagesViewController{})
// -------------------------------------- ------------------------------------------ // MARK: - 处理长按动作 // ---------------------------------------------- -------------------------------------- override func collectionView(_ collectionView: UICollectionView, shouldShowMenuForItemAt indexPath: IndexPath) -> Bool { return 真 }
override func collectionView(_ collectionView: UICollectionView, canPerformAction action: Selector, forItemAt indexPath: IndexPath, withSender sender: Any?) -> Bool { 打印(action.description) 让消息 = 消息[indexPath.section] //let message = messagesDataSource.messageForItem(at: indexPath, in: messagesCollectionView)
if action == NSSelectorFromString("reply:") { return true } else if action == NSSelectorFromString("edit:") { return true } else if action == NSSelectorFromString("forward:") { return true } else { return super.collectionView(collectionView, canPerformAction: action, forItemAt: indexPath, withSender: sender) }
}
override func collectionView(_ collectionView: UICollectionView, performAction action: Selector, forItemAt indexPath: IndexPath, withSender sender: Any?) {
if action == NSSelectorFromString("reply:") { print("reply item here!!") } else if action == NSSelectorFromString("edit:") { print("edit item here!!") } else if action == NSSelectorFromString("forward:") { print("forward item here!!") } else { super.collectionView(collectionView, performAction: action, forItemAt: indexPath, withSender: sender) }
}