我正在尝试将 tableView 设置为我的 viewController 中的 subview 。但是没有使用 Storyboard !我正在尝试这段代码,但它似乎不起作用,我不明白为什么.. 所以这是应该创建 TableView 的代码:
func TableView(position: CGRect, allRecipes: [Recipe]) -> UITableView {
let tableView: UITableView = UITableViewForAllRecipes(style: UITableViewStyle.Plain).tableView
return tableView;
}
.
这是我的 tableviewcontroller (UITableViewForAllRecipes)
class UITableViewForAllRecipes: UITableViewController {
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cell")
cell.textLabel.text = "ok"
return cell
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
println("nosit")
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
println("tv")
return 3
}
有人看到我做错了什么吗?因为它甚至似乎没有打印那些“nosit”和“tv”,所以它没有到达那个代码..我也没有从调试中得到任何东西..
最佳答案
如何实现将 TableView 添加到其 View 层次结构的 View Controller 取决于您是否需要单独的 View Controller 来管理 TableView 。
如果你确实需要一个单独的 View Controller ,那么</p>
class UITableViewForAllRecipes: UITableViewController {
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cell")
cell.textLabel.text = "ok"
return cell
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
println("nosit")
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
println("tv")
return 3
}
}
class ViewController: UIViewController {
let childTableViewController = UITableViewForAllRecipes(style: .Plain)
let otherView = UIView()
override func viewDidLoad() {
super.viewDidLoad()
// TODO: setup otherView's frame, autoresizingMask ...
view.addSubview(otherView)
addChildViewController(childTableViewController)
let tableView = childTableViewController.tableView
// TODO: setup tableView's frame, autoresizingMask ...
view.addSubview(tableView)
childTableViewController.didMoveToParentViewController(self)
}
}
否则
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
let tableView = UITableView()
let otherView = UIView()
override func viewDidLoad() {
super.viewDidLoad()
// TODO: setup otherView's frame, autoresizingMask ...
view.addSubview(otherView)
// TODO: setup tableView's frame, autoresizingMask ...
tableView.dataSource = self
tableView.delegate = self
view.addSubview(tableView)
}
// MARK: - UITableViewControllerDataSource
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cell")
cell.textLabel.text = "ok"
return cell
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
println("nosit")
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
println("tv")
return 3
}
}
关于swift - 在没有 Storyboard的情况下使用 UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26828695/