Is it possible to reassign an existing UITableView to another UITableViewController?

Most of my applications are built programmatically, but I have a few complex views that are built using Interface Builder and loaded programmatically.

In one view, I have a subclass of UITableViewController created programmatically and its tableView property is assigned to a UITableView instance in the XIB that is referenced as an IBOutlet. This happens in the XIV UIViewController viewDidLoad and the table view is displayed correctly.

At some point I need to change the entire contents of the table view. Ideally, I would like to detach this view from the first UITableViewController and attach it to another that will pass it to other data. Attempting to assign a view to a new controller's tableViewView property will result in "the view can only be associated with one view controller at a time!" Setting the previous controller tableView to zero before assigning the new method avoids the error, but the table view disappears from the screen.

To be honest, I know that a table view instance is usually managed by a UITableViewController. I can't find anything in the docs that setTableView automatically sets backreferences for the delegate and dataSource to the assigned tableView, so I'm surprised my tableView appearance works in the first place. In fact, the TVC docs seem to discuss the tableView property of the UITableViewController as readonly, even though it's not marked as such.

+3


source to share


2 answers


I'm sure I don't have all the information, but at first glance it seems that you are making this more difficult than necessary. Why don't you just use the current UITableViewController and the corresponding UITableView and reload it with "new data". Also, just programmatically create another UITableViewController and use it for new data. Is there a good reason to dispose of the UITableView?



+1


source


The answer is no , and here's why:

I checked with a debugger and the normal setTableView set the view property to itself and also set the delegate and dataSource properties on the new incoming tableView. This is not documented by Apple, but it was the intended behavior and it worked well and good.



I didn't understand that the default setView (from UIViewController) will remove the existing view from its supervisor before setting the new one. To prevent the "one view controller at a time" error, I set the old TVC tableView to nil, which made it necessary to reuse the tableView to be removed from the view hierarchy. To avoid this behavior, it would be necessary to override setView, which is more difficult than I would like to tackle this task.

Reusing views (in this case a UITableView instance) is not supported by Apple. It is not mentioned in the UITableViewController documentation, but it is mentioned in the UITableViewController documentation regarding the view property . Perhaps the tableView property of the UITableViewController should be marked read-only by Apple, its tableView is controlled exclusively internally, but there is probably a good reason why it is not.

+1


source







All Articles