WitaloBenicio
WitaloBenicio

Reputation: 3692

Use UIViewController as TableView cell

I have a segmented control which switches between two UIViewControllers. A info view, and a list view (TableView).

I want to use the first UIViewController as the first cell of my TableView (that is on the other segment).

Is there a way to convert a UIViewController to a cell or someway to use it as a cell for a TableView?

Upvotes: 9

Views: 12839

Answers (3)

Johno2110
Johno2110

Reputation: 1141

Swift 5.0 Syntax

let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for:indexPath)
cell.layoutIfNeeded()

let storyboard = UIStoryboard(name: "StoryboardName", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "ViewControllerNameHere")

self.addChild(vc)
cell.contentView.addSubview(vc.view)

vc.view.translatesAutoresizingMaskIntoConstraints = false
cell.contentView.addConstraint(NSLayoutConstraint(item: vc.view, attribute: NSLayoutConstraint.Attribute.leading, relatedBy: NSLayoutConstraint.Relation.equal, toItem: cell.contentView, attribute: NSLayoutConstraint.Attribute.leading, multiplier: 1.0, constant: 0.0))
cell.contentView.addConstraint(NSLayoutConstraint(item: vc.view, attribute: NSLayoutConstraint.Attribute.trailing, relatedBy: NSLayoutConstraint.Relation.equal, toItem: cell.contentView, attribute: NSLayoutConstraint.Attribute.trailing, multiplier: 1.0, constant: 0.0))
cell.contentView.addConstraint(NSLayoutConstraint(item: vc.view, attribute: NSLayoutConstraint.Attribute.top, relatedBy: NSLayoutConstraint.Relation.equal, toItem: cell.contentView, attribute: NSLayoutConstraint.Attribute.top, multiplier: 1.0, constant: 0.0))
cell.contentView.addConstraint(NSLayoutConstraint(item: vc.view, attribute: NSLayoutConstraint.Attribute.bottom, relatedBy: NSLayoutConstraint.Relation.equal, toItem: cell.contentView, attribute: NSLayoutConstraint.Attribute.bottom, multiplier: 1.0, constant: 0.0)) 

vc.didMove(toParent: self)
vc.view.layoutIfNeeded()

return cell

Upvotes: 4

bemeyer
bemeyer

Reputation: 6231

Well, i'd recomend to create a custom UITableViewCell with its own nib file and load it from nib.

This can than added/dequeud to a UITableViewSection. The Cell itself can then be design inside of the UI Builder in any way you want it to look like. You can put literaly everything into the cell you like.

Here is a short tutorial how todo so: using-nib-xib-files

A question to that topic can be find here: Custom UITableViewCell from nib in Swift

Upvotes: 0

Mahesh Agrawal
Mahesh Agrawal

Reputation: 3358

Use this code with your own way. Here we are adding the controllers view as subview of cell and using auto layout to manage properly. You just need to use the code by understanding.

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath:indexPath)
    cell.layoutIfNeeded()

    let infoVC = self.storyboard.instantiateViewControllerWithIdentifier("InfoVC")
    self.addChildViewController(infoVC)
    cell.contentView.addSubview(infoVC.view)

    infoVC.view.translatesAutoresizingMaskIntoConstraints = false
    cell.contentView.addConstraint(NSLayoutConstraint(item: infoVC.view, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: cell.contentView, attribute: NSLayoutAttribute.Leading, multiplier: 1.0, constant: 0.0))
    cell.contentView.addConstraint(NSLayoutConstraint(item: infoVC.view, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: cell.contentView, attribute: NSLayoutAttribute.Trailing, multiplier: 1.0, constant: 0.0))
    cell.contentView.addConstraint(NSLayoutConstraint(item: infoVC.view, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: cell.contentView, attribute: NSLayoutAttribute.Top, multiplier: 1.0, constant: 0.0))
    cell.contentView.addConstraint(NSLayoutConstraint(item: infoVC.view, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: cell.contentView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0.0))

    infoVC.didMoveToParentViewController(self)
    infoVC.view.layoutIfNeeded()
}

Upvotes: 17

Related Questions