0

the question may seem a little confusing so I'll simplify.

I have created a view controller (JournalViewController) that holds 2 container views (MealPlanViewController, ExerciseViewController - you can ignore this container). The MealPlanViewController has a UITableView and I want to be able to delete/move the rows of the UITableview by using the left navigation Edit button item. However, when I create the navigation item in the JournalViewController, I do not know how to make it editable with the container view's UITableView.

Here is an example:

enter image description here

Here is my JournalViewController class:

import UIKit

class JournalViewController: UIViewController {

@IBOutlet var exerciseContainerView: UIView!
@IBOutlet var mealContainerView: UIView!

@IBOutlet var mealOrExerciseControl: UISegmentedControl!

var mealScheduleTableView: UITableView?

override func viewDidLoad() {
    super.viewDidLoad()

    self.navigationItem.leftBarButtonItem = self.editButtonItem()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@IBAction func showComponent(sender: UISegmentedControl)
{
    if sender.selectedSegmentIndex == 0
    {
        UIView.animateWithDuration(0.5, animations:
        {
                self.exerciseContainerView.alpha = 0
                self.mealContainerView.alpha = 1
                self.navigationItem.leftBarButtonItem = self.editButtonItem()
        })
    }

    else
    {
        UIView.animateWithDuration(0.5, animations:
        {
                self.exerciseContainerView.alpha = 1
                self.mealContainerView.alpha = 0
                self.navigationItem.leftBarButtonItem = nil
        })
    }
}



}
user3171597
  • 447
  • 1
  • 6
  • 17

1 Answers1

0

In the mealPlanViewController class you would set an IBAction for when the user presses the edit button. Within the IBAction you can call any functions you want that impact the Tableview.

  @IBAction func editTable() {
          tableView.setEditing(true, animated: true)

  }

After this your TableDelegate functions take over for deleting and moving rows.

Steve Rosenberg
  • 19,348
  • 7
  • 46
  • 53
  • I thought about doing that, but if I do that, how would I call the `func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool` method? Since that method is needed to activate the options to delete and/or move tableview cells. – user3171597 Apr 10 '16 at 06:30
  • Thanks for the answer! And I apologize since I'm pretty new to this, but how do I connect the edit button programmatically from within the ScheduleViewController? Or do I need to specify and create the button inside of the storyboard itself? – user3171597 Apr 10 '16 at 21:21