Chen Li Yong Chen Li Yong - 3 months ago 10
Swift Question

How to make UITableView updating with delegate and datasource even if the view controller is a "child" controller?

I have two view controllers, let's call X, and Y. the X has a UIView called "container". the Y has a UIButton and UITableView with the delegate and datasource set to Y. After I initialise the Y view inside X, the X's container do

container.addSubView (Y.view)
. The button and tableview is shown, but the table view does not have any content at all. When I add break point, the
numberOfRowsInSection
and
cellForRowAtIndexPath
functions are not called at all. But if I open the Y view controller directly, the table view updates properly. Why is this happen? And how to fix it? Thanks.

EDIT: The code.

class Util: NSObject {
static func getViewControllerID(vcname: String, fromStoryboard storyboardName: String = "Main") -> UIViewController {
return UIStoryboard(name: storyboardName, bundle: nil).instantiateViewControllerWithIdentifier(vcname)
}
}

class X : UIViewController {

var tabs = [UIView]();
var tabIdx = 1;
var views = [UIView]();
@IBOutlet var container : UIView!;

override func viewDidLoad() {
super.viewDidLoad();

for i in 1...4 {
let tab = self.view.viewWithTag(i)!;
tab.backgroundColor = UIColor.clearColor();
tabs.append(tab);
}

views.append(Util.getViewControllerID("Dummy").view);
views.append(Util.getViewControllerID("Dummy").view);
views.append(Util.getViewControllerID("Y").view);
views.append(Util.getViewControllerID("Dummy").view);

changeActiveTab(0);
}

func changeActiveTab (idx : Int) {
tabIdx = idx;
for tab in tabs { tab.backgroundColor = UIColor.clearColor(); }
tabs[idx].backgroundColor = UIColor(hexString: "41C7DB");
for view in views { view.removeFromSuperview(); }
let view = views[idx];
view.frame = container.bounds;
container.addSubview(view);
}

@IBAction func tabTapped (sender: AnyObject) {
let btn = sender as! UIButton;
let idx = btn.tag - 11;
changeActiveTab(idx);
}
}


class Y : UIViewController, UITableViewDelegate, UITableViewDataSource {

var dummyData = [String]();

override func viewDidLoad() {
super.viewDidLoad()

for i in 101...106 {
let btn = self.view.viewWithTag(i)! as! UIButton;
btn.makeViewRoundedRectWithCornerRadius(4);
}

dummyData.append("a");
dummyData.append("b");
dummyData.append("c");
dummyData.append("d");
dummyData.append("e");
dummyData.append("f");
}

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

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5;
}

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

cell.backgroundColor = UIColor.clearColor();
var lbl = [UILabel]();
for i in 1...6 { lbl.append(cell.viewWithTag(i)! as! UILabel); }
for i in 0...5 { lbl[i].text = dummyData[i]; }

return cell;
}

}

Answer

When you add your childViewController as a subview

container.addSubview(view);

You also need move your childViewController to parent. Try to add this lines of code

self.addChildViewController(childViewController)
childViewController.didMoveToParentViewController(self)

And don't forget when you remove it from superView, just call

childViewController.removeFromParentViewController()
Comments