请查看我的代码并建议更正或我需要做的任何类型的代码更改,以便在选定的表格行上显示菜单。请帮助,我是iOS的新手。
import UIKit
class CutomerHomeTab: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
addCustomMenuItems()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func addCustomMenuItems() { //CUSTOM MENU IMPLEMENTED
let menuController = UIMenuController.sharedMenuController()
var newItems = menuController.menuItems ?? [UIMenuItem]()
newItems.append(UIMenuItem(title: "Abort", action: MenuAction.Abort.selector()))
newItems.append(UIMenuItem(title: "Accomplish", action: MenuAction.Accomplish.selector()))
menuController.menuItems = newItems
}
enum MenuAction:String{
case Accomplish = "Accomplish:" //ENUMERATED DATA TYPES DECLARATION FOR MENUACTION
case Abort = "Abort:"
//We need this awkward little conversion because «enum»'s can only have literals as raw value types. And «Selector»s aren't literal.
func selector()->Selector{
return Selector(self.rawValue)
//IMPLEMENTATIION OF SELECTOR FUNCTION
} //END OF SELECTOR FUNCTION
} //END OF ENUM DECLARATION
func Accomplish(sender:AnyObject?){
print("Did something new!accomplished") //FUNCTION FOR IMPLEMENTATION OF ACCOMPLISH
}
func Abort(sender:AnyObject?){
print("Did something new!aborted") //FUNCTION FOR IMPLEMENTATION OF ABORT
}
override func tableView(tableView: UITableView, canPerformAction action: Selector, forRowAtIndexPath indexPath: NSIndexPath, withSender sender: AnyObject?) -> Bool {//FUNCTION TO LET USER PERFORM ACTION ON THE GIVEN ROW
return action == MenuAction.Accomplish.selector() || action == MenuAction.Abort.selector()
}
override func tableView(tableView: UITableView, performAction action: Selector, forRowAtIndexPath indexPath: NSIndexPath, withSender sender: AnyObject?) {
//You can handle standard actions here, but custom actions never trigger this. It still needs to be present for the menu to display, though.
}
override func tableView(tableView: UITableView, shouldShowMenuForRowAtIndexPath indexPath: NSIndexPath) -> Bool { ///FUNCTION FOR SELECTION OF ROW
return true
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 7
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
return cell
}
}
答案 0 :(得分:1)
UIMenuController应用程序单例负责显示编辑操作菜单项,如复制,剪切,删除,选择等。关于它有很棒的NSHipster tutorial。我担心Accomplish和Abort实际上并不是编辑动作。所以我猜使用UIMenuController并不是完全正确的决定。如果您想为用户提供一些关于点击表视图单元格的选项,您最好使用UIActionSheet。如果你想以与UIMenuController完全相同的方式显示选项,我宁愿建议你考虑使用像https://github.com/camelcc/MenuPopOverView这样的CocoaPod。