1

I'm trying to develop a TableViewController that shows commands. Every cell of the table is a collectionView, but I'm wondering if I can programmatically instantiate a storyboard and pass the initial controller (collectionViewController) to that collectionView inside a UITableViewCell this a schema of what I'm trying to do.

The question is: Can I put a storyboard inside a tableViewCell so I can navigate inside that cell and do stuff ?

solution

 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
    let st = UIStoryboard(name: "Storyboard", bundle: nil)
    let vc = st.instantiateInitialViewController() as! UINavigationController

    self.addChildViewController(vc)
    vc.view.frame = CGRect(x:0, y:0,width: cell.contentView.frame.size.width, height: 40);
    cell.contentView.addSubview((vc.view)!)
    vc.didMove(toParentViewController: self)

    return cell
}

with Storyboard is a custom storyboard with navigationController as its first controller

El IBrahim
  • 13
  • 3

1 Answers1

0

You can open ViewController inside any UIView , if you want to instantiateViewController UITableCell that open in its contentView

let vc = self.storyboard.instantiateViewController(withIdentifier: "someViewController")
 self.addChildViewController(vc)
    vc.view.frame = CGRectMake(0, 0, cell.contentView.frame.size.width, self.container.frame.size.height);
    cell.contentView.addSubview(vc.view)
    vc.didMoveToParentViewController(self)

i hope this will help you

Dhiru
  • 3,040
  • 3
  • 25
  • 69
  • thanks, this worked fine with some changes to the syntax, I added a button to the `vc` so when I click on it it should navigate to a new controller, It did but it presented the new controller in the main view not in the tableCell – El IBrahim Jun 13 '17 at 11:45
  • just push the ViewController using NavigationController Don't persent – Dhiru Jun 13 '17 at 11:47
  • `navigator.pushViewController(viewController, animated: true)` that will work , Please make sure your controller is connected to navigationController in StoryBoard – Dhiru Jun 13 '17 at 11:48
  • https://stackoverflow.com/questions/39929592/how-to-push-and-present-to-uiviewcontroller-programmatically-without-segue-in-io visit this for pushing view , i hope this will help you. – Dhiru Jun 13 '17 at 11:50
  • yes it worked when I embed a navigation controller as first controller in my custom storyboard – El IBrahim Jun 13 '17 at 12:26