Please have a look at my code and suggest corrections or any kind of code alterations I need to do in order to get menu displayed on selected table row. Please help, I'm new to 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
}
}
UIMenuController application singleton is responsible for presenting edit action menu items such as Copy, Cut, Delete, Select and others. There is great NSHipster tutorial about it. I'm afraid Accomplish and Abort is not actually edit actions. So I guess using UIMenuController is not completely right decision. If you want to propose user some options on tapping table view's cell you should better use UIActionSheet. If you want to display options in the exact same way as UIMenuController does, I would rather suggest you to consider using some CocoaPod like https://github.com/camelcc/MenuPopOverView.