Search code examples
iosswiftuitableviewuitabbaritem

Swift: how to change Edit/Done buttons behaviour/function


In my iOS app I have a UITableViewController whose rows contain data and data picker. I want to change te function of the buttons Edit and Done. I would like the Edit button to allow the user to write/insert data (instead of deleting rows), while I would like the Done button to save to save the data (and not just exiting the edit mode). I've added to my code the following:

// The following two functions remove the red minus sign
override func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {
    return UITableViewCellEditingStyle.None
}

override func tableView(tableView: UITableView, shouldIndentWhileEditingRowAtIndexPath indexPath: NSIndexPath) -> Bool {
    return false
}

in order to avoid appearing the circle red minus, but how can I override the functions according to Edit or Done value of the button? I heard about delegates but I'm new to iOS and I don't know what they are...if someone can explain me I would be grateful.


Solution

  • Please try this code block :-)

    import UIKit
    
    // You can initialise an instance of this class manually or configure it on storyboard
    class TableViewController: UITableViewController {
        // MARK:- This part is for demo purpose
        override func viewDidLoad() {
            super.viewDidLoad()
            tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "Cell")
        }
    
        override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
            return 1
        }
    
        override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return 5
        }
    
        override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
            cell.backgroundColor = UIColor.yellowColor()
            cell.textLabel?.text = "\(indexPath.row)"
    
            return cell
        }
    
        // MARK:- From here is the main part to answer your question
        override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
            return true
        }
    
        override func tableView(tableView: UITableView, shouldIndentWhileEditingRowAtIndexPath indexPath: NSIndexPath) -> Bool {
            return false
        }
    
        override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {
    
            let editAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Edit") { (action, indexPath) -> Void in
                print("Write/Insert data here")
            }
    
            let doneAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Done") { (action, indexPath) -> Void in
                print("Save data here")
            }
            doneAction.backgroundColor = UIColor.blueColor()
    
            return [doneAction, editAction]
        }
    }
    

    You will see the result like this enter image description here