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

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设置为dataSourcetableView,并采用UITableViewDataSource协议。

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/

10-09 09:15