ios - UITableViewController 在模拟器中显示白色空白屏幕

标签 ios swift xcode uitableview uiviewcontroller

我正在尝试制作一个非常简单的 iOS 应用程序,它从一个 UITableViewController 开始,当您单击每个单元格时( children 应该学习的一种不同方式),它会将特定数据推送到 UIViewController。我在单元格中添加了占位符信息,但是当我运行模拟器时,只显示一个空白屏幕。我在下面附上图片和代码。

Main Storyboard

Simulator

class TableViewController: UITableViewController {

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


    @IBAction func backBtn(_ sender: AnyObject) {
        dismiss(animated: true, completion: nil);
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    override func numberOfSections(in tableView: UITableView) -> Int {
        return 0
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 0
    }

}

class ViewController: UIViewController, UITableViewDelegate {

    @IBOutlet var tableView: UITableView!

    var names = ["Manner 1", "Manner 2", "Manner 3", "Manner 4", "Manner 6", "Manner 7", "Manner 8"]

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 8
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var cell = self.tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath as IndexPath) as! Manner2Cell

        cell.name.text = names[indexPath.row]

        return cell
    }

}

//Manner2Cell actually refers to the first cell. I know I know bad naming convention.

class Manner2Cell: UITableViewCell {
    @IBOutlet weak var name: UILabel!

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

看着所有这些,我似乎有一些重复的代码。我觉得这很容易解决,但我就是想不通!

最佳答案

您必须将 ViewController 设置为 tableViewdataSource 并采用 UITableViewDataSource 协议(protocol)。

class ViewController: UITableViewDelegate, UITableViewDataSource, ... {
    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.delegate = self // Assigns ViewController as delegate for tableView 
        tableView.dataSource = self
    }

关于ios - UITableViewController 在模拟器中显示白色空白屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40960391/

相关文章:

iphone - 如何检测 iOS 设备是否支持模糊效果?

iphone - 打印出变量名

ios - 注释标注不起作用 Swift

ios - 从NSMutableDictionary构建NSMutableArray导致NSException

ios - 在后台线程中将核心数据从一个数据库复制到另一个数据库

ios - 两个词如果合适就放在同一行,如果不合适就换行

xcode - 如何禁用 iphone 3.5"屏幕 Xcode

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

xcode - 在 Swift 中从 NSString 创建 NSData

ios:如何在没有系留设备的情况下将新的 UDID 添加到配置文件?