简体   繁体   中英

UITableViewController showing white blank screen in simulator

I am trying to make a very simple iOS app that has begins at one UITableViewController and when you click on each cell (a different manner kids should learn) it pushes that specific data to a UIViewController . I have added placeholder information in the cells, but when I run the simulator only a blank screen shows up. I attach pictures and code below.

主故事板

仿真器

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
    }

}

Looking at all of this it looks like I have some repeated code. I feel like this is an easy fix but I just can't figure it out!

You have to set your ViewController as a dataSource for your tableView and adopt the protocol UITableViewDataSource .

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

The scene in the upper left of your Interface Builder screen snapshot looks like a UITableViewController (which is presumably the class you call TableViewController ).

But your numberOfSections and numberOfRowsInSection both return zero. So you'll have a blank white screen. Frankly, it looks like the code intended for the first scene's UITableViewController subclass has been put in the second scene's view controller.


Your second scene (upper right of your snapshot) is a little confusing, because it looks like that's a UITableViewController , too, but it doesn't look like it should be a table at all. I'm not sure what you're trying to do there. Is it really a table with repeated occurrences or is it just supposed to be the details of the cell you selected in the first scene? If it's just supposed to be a "details" screen, then that should be a simple UIViewController , not a UITableViewController .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM