Uitableview - Data from Disappearing Table

Asked

Viewed 100 times

0

I own a UITableView simple. It is loaded from a .nib and added as a subview, it appears, loads the data, presents the same everything correctly, but from the moment I select a cell, the data simply vanishes.

I’m currently using Swift, but I’ve had this same problem with Objective-C, which was solved without any code change, but now it is no longer being resolved.

Follow the creation code of UITableView:

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 subview:

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)

It is worth mentioning that the method didSelectRowAtIndexPath is not called

  • Why do you claim that the method didSelectRowAtIndexPath is not called? It will not be called if you have not defined the delegate of the table, was it? Because in this method you are removing all the view (and in it contains the table), that would be exactly the problem.

  • I put a breakpoint, it is not called and the view is not removed from the screen. The class is a Uitableviewcontroller

1 answer

1


Okay, I’ve come to an understanding of what you intend to do, and with that, you can’t just "extract" the UITableView from within your controller to put in another view.

You will also need to include the UITableViewController as "son" of the one who will receive it. I changed the way it is added, it was 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 method addChildViewController, that add the controller before adding to view that’s inside of her.

  • Thank you! The other times I did the same procedure I had not added her as "daughter", and still yes it had worked.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.