【问题标题】:Label not appearing in Swift table标签未出现在 Swift 表中
【发布时间】:2018-08-19 23:45:22
【问题描述】:

我的 Swift 表中没有出现任何数据。我对 Swift 还很陌生,不太清楚为什么会这样或我可能会丢失什么。我在很大程度上遵循了这里的指南,但有一些不同之处:

Apple Table Creation

这是 tableView 的定义:

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 AccountViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

这是我的表格单元格定义:

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。

我可以转到这个视图控制器所代表的帐户页面,并且确实出现了一个表格显示 - 它里面绝对没有数据。

我错过了什么?

【问题讨论】:

  • 您是否将视图控制器设置为 tableview 的数据源?你如何/在哪里加载userDataSource?设置断点查看numberOfRowsInSectioncellForRowAt是否被调用
  • AccountViewController 是场景的类。那是一样的吗?我会检查断点。
  • 您是否在使用之前注册了可重复使用的单元格?
  • @slickdaddy 我不太清楚这是什么意思
  • 向视图控制器添加表格视图时,必须先注册任何可重用单元格,然后才能将它们出列。在你的情况下:tableView.register(AccountTableViewCell.self, forCellReuseIdentifier: "AccountTableViewCell")

标签: ios swift uitableview controller storyboard


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2021-12-02
    • 2017-11-29
    • 1970-01-01
    • 1970-01-01
    • 2017-10-19
    • 2022-11-11
    • 1970-01-01
    • 1970-01-01
    • 2018-04-25
    相关资源
    最近更新 更多