Add the view of a UIViewController as a subview of a UITableViewCell?

0

I'm trying to create a simple table, and in a cell the content should be the view of a UIViewController, which has another UITableView.

But the cell goes blank, I tried several different ways. But I did not find a solution.

Below my last test code that served to ensure that a view is delivered to the cell, however it is shown in white. Without the UITableView.

if indexPath.section == 5 {
        let cell = tableView.dequeueReusableCellWithIdentifier("CellLimpa", forIndexPath: indexPath) as! UITableViewCell
        let stBoard = UIStoryboard(name: "Main", bundle: nil)
        let lsCarrosVC = stBoard.instantiateViewControllerWithIdentifier("listaDeCarrosComplexa") as! ListaDeCarrosComplexaViewController
        cell.backgroundColor = UIColor.blueColor()

        lsCarrosVC.view.frame = CGRect(x: 0, y: 0, width: 300, height: 300)
        lsCarrosVC.view.backgroundColor = UIColor.greenColor()
        lsCarrosVC.tabelaCarros.frame = CGRect(x: 0, y: 0, width: 300, height: 300)

        cell.conteudoView.backgroundColor = UIColor.blueColor()

        cell.conteudoView.addSubview(lsCarrosVC.view)


        return cell

      }

Result:

The green area is the ListaDeCarrosComplexaViewController view. And the blue area is the bottom of the cell.

    
asked by anonymous 10.08.2016 / 00:39

1 answer

0

Hello James, what do you think of when you instantiate a UIViewController

    let stBoard = UIStoryboard(name: "Main", bundle: nil)
    let lsCarrosVC = stBoard.instantiateViewControllerWithIdentifier("listaDeCarrosComplexa") as! ListaDeCarrosComplexaViewController 

Your views are not automatically loaded.

The methods that do this should be called when you present the same. To test include Breakpoints in the dataSource methods of the table present in the ControllerComplexViewController, they probably will not be called with this implementation.

In swift I never used but in Obj-C I always forced the controller to load its views before using them, I did not test but it should be this method that does the necessary work.

    if indexPath.section == 5 {
    let cell = tableView.dequeueReusableCellWithIdentifier("CellLimpa", forIndexPath: indexPath) as! UITableViewCell
    let stBoard = UIStoryboard(name: "Main", bundle: nil)
    let lsCarrosVC = stBoard.instantiateViewControllerWithIdentifier("listaDeCarrosComplexa") as! ListaDeCarrosComplexaViewController

    lsCarrosVC.loadView() // Acrescente essa chamada!

    cell.backgroundColor = UIColor.blueColor()

    lsCarrosVC.view.frame = CGRect(x: 0, y: 0, width: 300, height: 300)
    lsCarrosVC.view.backgroundColor = UIColor.greenColor()
    lsCarrosVC.tabelaCarros.frame = CGRect(x: 0, y: 0, width: 300, height: 300)

    cell.conteudoView.backgroundColor = UIColor.blueColor()

    cell.conteudoView.addSubview(lsCarrosVC.view)


    return cell

  }

Getting away from your question if your table is dynamic beware of instantiating a controller within the CellForRowAtIndexPath method this will reduce the performance of your App.

    
10.08.2016 / 21:25