即使视图控制器是 "child" 控制器,如何使用委托和数据源更新 UITableView?
How to make UITableView updating with delegate and datasource even if the view controller is a "child" controller?
我有两个视图控制器,我们称之为 X 和 Y。X 有一个名为 "container" 的 UIView。 Y 有一个 UIButton 和 UITableView,委托和数据源设置为 Y。在我初始化 X 内的 Y 视图后,X 的容器执行 container.addSubView (Y.view)
。显示了按钮和 table 视图,但 table 视图根本没有任何内容。当我添加断点时,根本没有调用 numberOfRowsInSection
和 cellForRowAtIndexPath
函数。但是如果我直接打开 Y 视图控制器,table 视图会正确更新。为什么会这样?以及如何解决?谢谢。
编辑:代码。
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;
}
}
当您将 childViewController
添加为 subview
container.addSubview(view);
您还需要将 childViewController
移至父级。尝试添加这行代码
self.addChildViewController(childViewController)
childViewController.didMoveToParentViewController(self)
并且不要忘记从 superView 中删除它时,只需调用
childViewController.removeFromParentViewController()
我有两个视图控制器,我们称之为 X 和 Y。X 有一个名为 "container" 的 UIView。 Y 有一个 UIButton 和 UITableView,委托和数据源设置为 Y。在我初始化 X 内的 Y 视图后,X 的容器执行 container.addSubView (Y.view)
。显示了按钮和 table 视图,但 table 视图根本没有任何内容。当我添加断点时,根本没有调用 numberOfRowsInSection
和 cellForRowAtIndexPath
函数。但是如果我直接打开 Y 视图控制器,table 视图会正确更新。为什么会这样?以及如何解决?谢谢。
编辑:代码。
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;
}
}
当您将 childViewController
添加为 subview
container.addSubview(view);
您还需要将 childViewController
移至父级。尝试添加这行代码
self.addChildViewController(childViewController)
childViewController.didMoveToParentViewController(self)
并且不要忘记从 superView 中删除它时,只需调用
childViewController.removeFromParentViewController()