Adding a childview manager view to a dynamic UITableViewCell
How can I add a controller view of a child view to a custom one UITableViewCell
? I can add a 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 that child view controller. I'm not sure how to do this. Is there a better way to do this?
source to share
Don't understand MVC. Not every point of view in the world needs its own personal controller! The main view has a view controller, but the button in that main view does not have its own personal view controller; it just talks to the main view controller.
The same applies to this view. Views can come and go very easily; don't add the heavy burden of an additional view controller when you don't need it! Just take a view (somehow) and insert it into a cell, contentView
or remove it from a cell contentView
in cellForRowAtIndexPath:
, like any other view, but manage it with a table view controller or table data source / delegate or whatever here. Don't add an extra controller view to your history just for the sake of this small view. This is probably bad use of view controllers.
source to share
Do it through delegation. I did it in collection view mode, you can do it in tableview too. follow the steps below.
1. In your custom cell class, create a Handler delegate and override the awakeFromNib () method. eg,
protocol BibleReadingSliderProtocol: class {
func addThisViewControllerAsChild(audioViewController :AudioViewController)
}
class BibleReadingSliderCollectionCell: UICollectionViewCell {
@IBOutlet weak var containerView: UIView!
var audioVC = AudioViewController()
weak var bibleReadingSliderDelegate:BibleReadingSliderProtocol?
override func awakeFromNib() {
super.awakeFromNib()
print("Awake call from cell")
// Initialization code
let storyboard = UIStoryboard(name: "Main", bundle: nil)
audioVC = storyboard.instantiateViewController(withIdentifier: "AudioViewController") as! AudioViewController
audioVC.view.frame = self.containerView.bounds
self.containerView.addSubview(audioVC.view)
if self.bibleReadingSliderDelegate != nil {
self.bibleReadingSliderDelegate?.addThisViewControllerAsChild(audioViewController: audioVC)
}
}
}
-
In your ViewController where you are using this custome cell (table or collection view) define a delegate delegate
func addThisViewControllerAsChild(audioViewController: AudioViewController) { self.addChildViewController(audioViewController); }
And don't forget to set your delegate to this view manager at cellForItemAt / cellForRowAt
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let imageSliderCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! BibleReadingSliderCollectionCell
imageSliderCollectionViewCell.bibleReadingSliderDelegate = self
return imageSliderCollectionViewCell
}
source to share