gpt4 book ai didi

swift - Tableview 不显示我的附加数组,即使它们不为空

转载 作者:行者123 更新时间:2023-11-30 12:22:11 25 4
gpt4 key购买 nike

我的问题是这样的:我可以从 firebase 加载所有内容,我可以将其附加到不同的数组中。但我无法让它显示在我的桌面 View 中。当我使用section时它不起作用,但是如果我将静态字符串插入到我的数组之一中,例如:accidentMessages:String = [“Hu”],那么Hu明显地在 TableView 中的“accident”部分下在控制台中,它从 firebase 打印出我的消息,因此字符串(消息)必须已附加到我的数组中。但由于某种原因我无法在桌面 View 中展示它。这是从 firebase 获取并添加到数组中的消息的控制台日志。 enter image description here

消息1应该在“帮助”中,消息2应该在威胁中,消息3应该在事故中。但不显示,只在控制台显示

var helpMessage: [String] = []
var threatMessage: [String] = []
var accidentMessage: [String] = ["Hu"]

var sectionMessages: [Int: [String]] = [:]
let sectionTitle = ["Help:", "Threat:", "Accident"]


sectionMessages = [0: helpMessage, 1: threatMessage, 2: accidentMessage]

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

return (sectionMessages[section]?.count)!
}

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let view = UIView()
view.backgroundColor = UIColor.orange

let image = UIImageView(image: sectionImages[section])
image.frame = CGRect(x: 5, y: 5, width: 35, height: 35)
view.addSubview(image)

let label = UILabel()
label.text = sectionTitle[section]
label.frame = CGRect(x: 45, y: 5, width: 100, height: 35)
view.addSubview(label)

return view

}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 45
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: "cell")

if cell == nil {
cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
}

cell!.textLabel?.text = sectionMessages[indexPath.section]![indexPath.row]

return cell!
}

最佳答案

sectionMessages 包含 3 个数组的副本,使用您为 sectionMessages 分配值时这些数组所具有的任何值。

如果您在初始化sectionMessages后需要修改这些数组,那么您需要直接在sectionMessages中修改它们,而不是修改原始数组。

或者你可以放弃 sectionMessages 并使用类似的东西

func messages(for section: Int) -> [String] {
switch section {
case 0: return helpMessage
case 1: return threatMessage
case 2: return accidentMessage
default: return []
}

关于swift - Tableview 不显示我的附加数组,即使它们不为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44664856/

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