Swift TableView 人口

Swift TableView Population

我有一个 table 视图控制器,我正在使用 Storyboard 进行设计。原型单元非常基本,只有两个标签。姓名标签和日期标签。

当我 运行 我的程序时,样式在那里(字体等),但似乎约束不起作用?此外,单元格颜色不显示(仅显示 table 视图背景),并且缺少单元格分隔符。标签彼此重叠,当我将单元格高度设置为 75 时,它看起来并不那么高。

这是我的单元格编译后的样子: See tableview here

我为 table 视图单元制作了一个模型,如下所示(IdeaCell 已正确设置在情节提要中的原型单元上):

class IdeaCell: UITableViewCell {

    @IBOutlet weak var ideaNameLbl: UILabel!
    @IBOutlet weak var ideaDateLbl: UILabel!


    override func awakeFromNib() {
        super.awakeFromNib()
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        if selected {
            self.layer.backgroundColor = UIColor(white: 1, alpha: 0.2).cgColor
        } else {
            self.layer.backgroundColor = UIColor.clear.cgColor
        }
    }

    func configureCell(idea : CoreIdea) {
        let name = idea.name
        ideaNameLbl.text = name

        let date = idea.date
        ideaDateLbl.text = date
    }
}

我的 tableview view controller 相关代码在这里(重用标识符 (ideaCell) 已在情节提要中的原型单元格上正确设置):

class IdeaVC: UITableViewController { 
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if let cell = tableView.dequeueReusableCell(withIdentifier: "ideaCell", for: indexPath) as? IdeaCell {
            let idea = ideas[indexPath.row]
            cell.configureCell(idea: idea)
            return cell
        } else {
            return UITableViewCell()
        }
    }
}

更新: 添加 heightForRowAt 函数后,我的单元格高度合适,并且约束正常工作。由于某种原因,单元格背景颜色丢失,并且单元格分隔线没有出现(字体和字体颜色有效)。他们都在故事板中设置。

这是我的单元格的屏幕截图: missing cell style

更新 2: 我这样设置分隔线和背景:

self.tableView.backgroundColor = UIColor.black
self.tableview.separatorColor = UIColor.white

希望这对某人有所帮助!老实说,我不知道为什么故事板中的值不起作用,但将它们设置在 viewDidLoad 中效果很好。

你告诉你的tableview uitableviewcell 的高度了吗?如果没有试试这个。这是您的 uitableview 委托方法之一。

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 75.0 //Set your cell height that you want to appear in your tableview
}

我认为您必须在函数中覆盖 Item 的数量和行的高度。并在名称和日期标签之间设置 UITableViewCell 的约束。