2015-12-30 55 views
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 
} 




} 

回答

1

UIMenuController應用單是負責提出編輯操作的菜單項,如複製,剪切,刪除,選擇等。關於它有很大的NSHipster tutorial。恐怕完成和放棄實際上不是編輯操作。所以我想用UIMenuController並不是完全正確的決定。如果你想建議用戶點擊表格視圖單元格的某些選項,最好使用UIActionSheet。如果您想以與UIMenuController完全相同的方式顯示選項,我寧願建議您考慮使用一些CocoaPod,如https://github.com/camelcc/MenuPopOverView

+0

我可以創建自定義項目,如中止或完成 –

+0

當然。你可以在NSHipster的文章UIMenuItem部分找到關於這方面的信息。但從UX方面來看,這不是一個正確的決定。 – iyuna