0
請查看我的代碼並提出更正或任何類型的代碼更改,以便讓菜單顯示在選定的表格行上。請幫忙,我是iOS新手。UITableviewController不會顯示觸摸菜單
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
}
}
我可以創建自定義項目,如中止或完成 –
當然。你可以在NSHipster的文章UIMenuItem部分找到關於這方面的信息。但從UX方面來看,這不是一個正確的決定。 – iyuna