How can I add a view of a child view controller to a custom UITableViewCell
? I can add the view like this inside cellForRowAtIndexPath
:
self.addChildViewController(controlsViewController)
cell!.cellView.addSubview(controlsViewController.view)
controlsViewController.didMoveToParentViewController(self)
But when the cell disappears, I need to remove this child view controller. I'm not really sure how to do that. Is there a better way to go about this?
Don't misunderstand MVC. Not every view in the world needs to have its own personal view controller! A main view has a view controller, but a button in that main view does not have its own personal view controller; it simply talks to the main view's view controller.
The same is true of this view. Views can come and go very easily; do not add the heavyweight burden of an additional view controller when you don't need to! Just grab the view (somehow) and stick it into the cell's
contentView
or remove it from the cell'scontentView
incellForRowAtIndexPath:
, just like any other view - but manage it using your table view controller or table view data source / delegate or whatever is in charge here. Don't add an extra view controller to the story just for the sake of this one little view. That's likely to be a bad use of view controllers.