swift - 在没有 Storyboard的情况下使用 UITableView

标签 swift uitableview ios8 tableview

我正在尝试将 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/

相关文章:

ios - 如何在多个标记之间绘制折线?

ios - 延迟在 UITableView 中插入行

ios - UIActionsheet 委托(delegate)方法在 ios8 中调用两次?

swift - 有什么方法可以确定 adjustsFontSizeToFitWidth 是否已触发?

iphone - UITableView 像 Xcode 中的 gridview 一样吗?

ios - 如何使用 AFNetworking 2.0 将包含字典的数组发送到服务器?

ios - 无法使用mockURLSession调用非函数类型 'URLSession'错误的值

ios - 体系结构 x86_64 iOS swift 的 undefined symbol

ios - Swift 3 NSMutableURLRequest 第二次调用失败

xcode - 从自定义单元格中调用 TableViewController 中的方法