UITableView不显示任何单元格

问题描述 投票:0回答:1

我有一个ViewController,它通过Interface Builder设置了UITableView(和一种类型的UITableViewCell)。我把它连接到一些IBOutlets

@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
    super.viewDidLoad()
    tableView.delegate = self
    tableView.allowsSelection = false
    tableView.dataSource = self
    tableView.register(CommentTableViewCell.self, forCellReuseIdentifier: CommentTableViewCell.identifier)
    tableView.rowHeight = UITableViewAutomaticDimension
    tableView.estimatedRowHeight = 150
}

然后我实现了以下方法:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    print(self.comments.count)
    return self.comments.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    print(self.comments[indexPath.row], indexPath.row)
    let cell = tableView.dequeueReusableCell(withIdentifier: CommentTableViewCell.identifier, for: indexPath) as! CommentTableViewCell
    cell.comment = self.comments[indexPath.row]
    print(cell)
    return cell

}

func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
    let comment = comments[indexPath.row]
    return comment.user.isMe ? .delete : .none
}


func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    guard editingStyle == .delete, !isDeleting else {
        return
    }

    isDeleting = true

    let comment = self.comments[indexPath.row]
    comment.delete() { result in
        self.isDeleting = false

        switch result {
        case .success:
            self.comments.remove(at: indexPath.row)
            tableView.deleteRows(at: [indexPath], with: .automatic)
        case .failure:
            self.displayError("Could not delete comment")
        }
    }
}

现在这看起来很简单(用于调试目的的print语句),但是,tableView实际上不会显示任何单元格。在调用tableView.reloadData()之后,numberOfRowsInSection方法返回2. cellForRowAt还打印2个CommentTableViewCell

但是这些都没有显示出来。经过一些测试后,我发现显示了tableview本身。

现在为什么会发生这种情况呢?我不认为我错过了什么。有任何人对此有经验吗?

谢谢 :-)

ios swift uitableview
1个回答
0
投票

假设CommentTableViewCell具有此comment标签,请确保正确设置标签的垂直约束。问题似乎是关于不正确的细胞高度。

正如Reinier Melian所建议的那样,尝试固定的高度,看看是否有效。如果没有,暂时将单元格更改为UITableViewCell,并设置标题标签,使其工作。这样,我们可以更好地隔离问题。

© www.soinside.com 2019 - 2024. All rights reserved.