gpt4 book ai didi

ios - Swift TableView 人口

转载 作者:行者123 更新时间:2023-11-29 05:24:32 24 4
gpt4 key购买 nike

我有一个表格 View Controller ,我正在使用 Storyboard设计它。原型(prototype)单元非常基本,它只有两个标签。姓名标签和日期标签。

当我运行程序时,样式就在那里(字体等),但似乎约束不起作用?此外,单元格颜色不显示(仅显示表格 View 背景),并且单元格分隔线丢失。标签相互重叠,当我将单元格高度设置为 75 时,它看起来并没有那么高。

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

我为 TableView 单元格创建了一个模型,如下所示(IdeaCell 在 Storyboard 中的原型(prototype)单元格上正确设置):

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)在 Storyboard中的原型(prototype)单元上正确设置):

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 函数后,我的单元格具有适当的高度,并且约束正常工作。由于某种原因,单元格背景颜色丢失,并且单元格分隔线没有出现(字体和字体颜色有效)。它们都被设定在 Storyboard中。

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

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

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

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

最佳答案

你有没有告诉你的tableview tableviewcell的高度?如果没有尝试这个。这是您的 uitableview 委托(delegate)方法之一。

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

关于ios - Swift TableView 人口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58313741/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com