Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Child View Controller view to dynamic UITableViewCell

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?

like image 343
Tometoyou Avatar asked Jun 12 '15 22:06

Tometoyou


2 Answers

Do it via delegation. I have done on collection view ,you can do it on tableview too. follow the below steps

1 .In your custom cell class create a delegateHandler and override your 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)
        }

    }
}
  1. In your ViewController where you are using this custome cell (either tableview or collection view) define the delegate hander

    func addThisViewControllerAsChild(audioViewController: AudioViewController) {
        self.addChildViewController(audioViewController);
     }
    

And Dont forget to set your delegate to this viewcontroller in 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
}
like image 82
Chandramani Avatar answered Nov 16 '22 02:11

Chandramani


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's contentView in cellForRowAtIndexPath:, 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.

like image 23
matt Avatar answered Nov 16 '22 02:11

matt