UITableView - Table data disappearing

0

I have a% simple%. It is loaded from a UITableView and added as a .nib , it appears, loads the data, displays the same all correctly, however from the moment I select a cell, the data simply disappears. >

I'm currently using Swift, but I already had this same problem with Objective-C, which was resolved without any code changes , but now is no longer being resolved.

The creation code of subview follows:

override func viewDidLoad() {
        super.viewDidLoad()
        self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "Cell")

        listObjectsToList = ["String 1","String 2","String 3"]

    self.view.frame.size = CGSizeMake(300, 110)
    self.view.layer.cornerRadius = 5
    self.view.layer.masksToBounds = true

    self.tableView.scrollEnabled = false
}

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return listObjectsToList.count
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell: UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("Cell") as UITableViewCell
    cell.textLabel.text = listObjectsToList.objectAtIndex(indexPath.row) as? String
    return cell
}

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return 40
}

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    self.tableView.deselectRowAtIndexPath(indexPath, animated: true)
    let objectSelected: AnyObject = listObjectsToList.objectAtIndex(indexPath.row)
    object.type = objectSelected as String
    self.view.removeFromSuperview()
}

Addition code as UITableView :

var listTypeTableViewController = NSBundle.mainBundle().loadNibNamed("TableViewTypeScheduling", owner: self, options: nil)[0] as ListTypeTableViewController
listTypeTableViewController.view.frame = CGRectMake(10, 80, 300, 130)
self.view.addSubview(listTypeTableViewController.view)

Note that the subview method is not called

    
asked by anonymous 20.11.2014 / 12:12

1 answer

1

Ok, I've come to the understanding of what you intend to do, and with that, you can not simply "extract" UITableView from within your controller to put in another view .

You will also need to include UITableViewController as the "child" of the child that will receive it. I changed the way it is added, it looks like this:

var listTypeTableViewController = ListTypeTableViewController(nibName: "TableViewTypeScheduling", bundle: nil)        
listTypeTableViewController.view.frame = CGRectMake(10, 80, 300, 130)        
addChildViewController(listTypeTableViewController)
view.addSubview(listTypeTableViewController.view)

The big point is the addChildViewController method, which adds the controller before adding the view that is inside it.

    
20.11.2014 / 13:18