标签未出现在 Swift table 中

Label not appearing in Swift table

我的 Swift table 中没有数据。我是 Swift 的新手,不太确定为什么会这样或我可能会遗漏什么。我大部分时间都遵循了这里的指南,但有一些不同:

Apple Table Creation

这是 table 视图定义:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cellIdentifier = "AccountTableViewCell"
        guard let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? AccountTableViewCell else {
            fatalError("The dequeued cell is not an instance of AccountTableViewCell.")
        }

        let item = userDataSource[indexPath.row]
// Dummy values just to test this out
        cell.leftLabel.text = "test1";
        cell.rightLabel.text = "test2";

        return cell
    }

func numberOfSections(in tableView: UITableView) -> Int {
    return 1;
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) ->Int {
    return userDataSource.count;
// This should be an array value, but I have also tried passing a static int here as well to test
}

这是我的 class 定义和实现的协议:

class AccountViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

这是我的 table 单元格定义:

class AccountTableViewCell: UITableViewCell {

    //MARK: Properties
    @IBOutlet weak var leftLabel: UILabel!
    @IBOutlet weak var rightLabel: 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
    }

}

我在 Storyboard 中设置了 rightLabel 和 leftLabel。

我可以转到此视图控制器所表示的帐户页面,并且确实会出现 table 显示 - 它完全没有数据。

我错过了什么?

仅向视图控制器场景添加 UITableView 是不够的。您必须在表视图的 Storyboard 连接检查器中将表视图的 dataSource 属性 设置为您的视图控制器实例。