stephen D
stephen D

Reputation: 265

How to swipe to edit text in a tableView?

In my app I have a tableView in witch I have default text and I would like to have a swipe to edit text function so the user can change the text if the want to. I already added the swipe to delete, using the code below, however adding an editing text function isn't as easy to find information on, So my question is, How can I add a button so the user can edit the text via a swipe function? I need to know the code to load the text as I can't have a textfield so it's not as easy as something like label.text = textfield.text the code that original loads the text is as follows

 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) 

        cell.textLabel?.text = places[indexPath.row]["name"]

Thanks !

 override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {

        if editingStyle == UITableViewCellEditingStyle.Delete {

            places.removeAtIndex(indexPath.row)

            NSUserDefaults.standardUserDefaults().setObject(places, forKey: "places")

            tableView.reloadData()

        }
           }

Upvotes: 1

Views: 1118

Answers (1)

buxik
buxik

Reputation: 2625

func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {  

  var editAction = UITableViewRowAction(style: .Normal, title: "Edit") { (action, indexPath) in
                tableView.editing = false

    // your action
            }

            editAction.backgroundColor = UIColor.grayColor()


    var deleteAction = UITableViewRowAction(style: .Default, title: "Delete") { (action, indexPath) in
                tableView.editing = false
    // your delete action
    }

    return [deleteAction, editAction]

Upvotes: 1

Related Questions