Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift / Enable Editing Mode in View Controller

Because of UI elements, I created View Controller with a TableView inside. But I can't enable editing mode. I tried several methods without Solution. But with using TableView Controller there are no problems.

What I tried:

override func viewDidLoad() {
  self.navigationItem.rightBarButtonItem = self.editButtonItem()
}

override func setEditing(editing: Bool, animated: Bool) {
 super.setEditing(editing, animated: animated)
}

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        if (editingStyle == UITableViewCellEditingStyle.Delete) {
            // Action to delete data
        }
    }

Where can be the problem?

like image 485
mostworld77 Avatar asked Jun 06 '15 22:06

mostworld77


People also ask

How do I change the view controller in Swift?

Right-click the control or object in your current view controller. Drag the cursor to the view controller you want to present. Select the kind of segue you want from the list that Xcode provides.

How do I make my view controller scrollable?

One way to do this is programmatically create an UIScrollView in your UIViewController . To control the scrollability you can set the ScrollView contentSize property.

What is view controller in Swift?

A view controller manages a single root view, which may itself contain any number of subviews. User interactions with that view hierarchy are handled by your view controller, which coordinates with other objects of your app as needed. Every app has at least one view controller whose content fills the main window.


1 Answers

You forgot to put the table view in editing mode:

override func setEditing(editing: Bool, animated: Bool) {
    super.setEditing(editing, animated: animated)
    self.tableView.setEditing(editing, animated: animated)
}

I'm assuming you have a tableView property. Adjust as needed.

Some other things you may want to do to emulate a UITableViewController:

  1. In viewWillAppear you should deselect the currently selected row in the table view.
  2. In viewDidAppear you should flash the scrollbars of the table view.
like image 106
rmaddy Avatar answered Oct 04 '22 11:10

rmaddy