首页 > 解决方案 > UITableView 单元格不显示所有数据

问题描述

UITableView从 JSON 文件填充第一个。一切似乎都在加载,没有崩溃,但表格单元格不包含所有数据。它们在底部被切断。

HeroViewController

class HeroViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, WCSessionDelegate {

    // code

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let tableCell : HeroTableViewCell = tableView.dequeueReusableCell(withIdentifier: "MessageCell") as? HeroTableViewCell ?? HeroTableViewCell(style: UITableViewCell.CellStyle.default, reuseIdentifier: "MessageCell")

        let row = indexPath.row
        let rowObj = messageObject[row]

        tableCell.one.text = rowObj.title as String?
        tableCell.two.text = rowObj.speaker as String?
        tableCell.three.text = rowObj.from as String?
        tableCell.four.text = rowObj.to as String?

        return tableCell
    }
}

标签: iosswiftuitableview

解决方案


你的细胞似乎没有足够的高度。您可以通过覆盖此方法来设置它:

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 100; //return the cell height you desire 
}

推荐阅读