evanhaus
evanhaus

Reputation: 747

(Swift) Show TableViewController in a ViewController's Container view

I am trying to get a tableview controller to display inside of a container view inside of a view controller. This is my storyboard set up:

enter image description here

enter image description here

These are my swift files:

Here is some code for my page view swiping:

  func getStepZero() -> ProfileViewController {
    return storyboard!.instantiateViewControllerWithIdentifier("ProfileView") as! ProfileViewController
  }

  func getStepOne() -> HomeViewController {
    return storyboard!.instantiateViewControllerWithIdentifier("HomeView") as! HomeViewController
  }

  func getStepTwo() -> MatchesSegueViewController {
    return storyboard!.instantiateViewControllerWithIdentifier("MatchesSegueView") as! MatchesSegueViewController
  }

  func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {

    if viewController.isKindOfClass(MatchesSegueViewController) {
      // 2 -> 1
      return getStepOne()
    } else if viewController.isKindOfClass(HomeViewController) {
      // 1 -> 0
      return getStepZero()
    } else {
      // 0 -> end of the road
      return nil
    }
  }

  func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {

    if viewController.isKindOfClass(ProfileViewController) {
      // 0 -> 1
      return getStepOne()
    } else if viewController.isKindOfClass(HomeViewController) {
      // 1 -> 2
      return getStepTwo()
    } else {
      // 2 -> end of the road
      return nil
    }
  }

tableview code:

class MatchTableViewController: UITableViewController {

  let cellId = "cellId"

override func viewDidLoad() {
    super.viewDidLoad()

    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = false

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem()
}

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

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 0
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return 5
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    // line below comes with function
    //let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath)

    // Configure the cell...
    // lines below are from YouTube tutorial
  let cell = UITableViewCell(style: .Subtitle, reuseIdentifier: cellId)
  cell.textLabel?.text = "DUMMY TEXT"

    return cell
}

I have a page view controller so when I swipe right I go to my view controller which should immediately segue to my table view controller (hopefully the segue won't be seen) via an embed segue. How can I accomplish this? When I swipe right it shows me an empty table view even though it should show some dummy data cells.

Any help is greatly appreciated.

Upvotes: 0

Views: 628

Answers (1)

Ali Riahipour
Ali Riahipour

Reputation: 524

From your code,

Change this section of your tableViewController

 override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
      return 0
 }

To this

 override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
      return 1
 }

Upvotes: 2

Related Questions