gpt4 book ai didi

ios - 为什么数据没有填充到我的自定义 ui TableView 单元格中

转载 作者:搜寻专家 更新时间:2023-10-31 22:34:04 25 4
gpt4 key购买 nike

我正在从服务器获取数据,我想把它放在我的自定义 UITableViewCell 中

这是 Storyboard中的单元格

enter image description here

如你所见,有两件事:

  1. 偏好标签
  2. 三个按钮

当我从服务器接收到数据时,我会这样做:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("OneResponseTableViewCell") as! OneResponseTableViewCell
let oneResponse = self.responses[indexPath.row]
cell.preferencesLabel?.text = oneResponse.restaurantName
print("row = \(indexPath.row), timesOptions = \(oneResponse.timeOptions)")
if oneResponse.timeOptions.count >= 1{
cell.firstTimeOption!.titleLabel?.text = oneResponse.timeOptions[0];
}
if oneResponse.timeOptions.count >= 2 {
cell.secondTimeOption!.titleLabel?.text = oneResponse.timeOptions[1];
}
if oneResponse.timeOptions.count >= 3 {
cell.thirdTimeOption!.titleLabel?.text = oneResponse.timeOptions[2];
}
return cell
}

如你所见,我正在链接标签和按钮,

但是,按钮并没有改变,请看结果:

enter image description here

我试着像你在代码中看到的那样输入打印,打印的数据是正确的,请看

row = 0, timesOptions = ["7:30 pm", "8:30 pm", "9:00 pm"]
row = 1, timesOptions = ["11:30 am", "12:00 pm", "12:30 pm"]
row = 2, timesOptions = ["7:30 pm", "8:30 pm", "9:00 pm"]
row = 3, timesOptions = ["7:30 pm", "8:30 pm", "9:00 pm"]

为什么它在单元格中不正确?

抱歉我的英语不好

最佳答案

设置按钮标题时,您应该使用setTitle(forState:)。例如:

if oneResponse.timeOptions.count >= 1 {
cell.firstTimeOption.setTitle(oneResponse.timeOptions[0], forState: .Normal)
}
if oneResponse.timeOptions.count >= 2 {
cell.secondTimeOption.setTitle(oneResponse.timeOptions[1], forState: .Normal)
}
if oneResponse.timeOptions.count >= 3 {
cell.thirdTimeOption.setTitle(oneResponse.timeOptions[2], forState: .Normal)
}

顺便说一句,因为您正在重用单元格,所以您确实应该检查这些 if 语句的 else 子句,如果失败则隐藏按钮:

if oneResponse.timeOptions.count >= 1 {
cell.firstTimeOption.setTitle(oneResponse.timeOptions[0], forState: .Normal)
cell.firstTimeOption.hidden = false
} else {
cell.firstTimeOption.hidden = true
}
if oneResponse.timeOptions.count >= 2 {
cell.secondTimeOption.setTitle(oneResponse.timeOptions[1], forState: .Normal)
cell.secondTimeOption.hidden = false
} else {
cell.secondTimeOption.hidden = true
}
if oneResponse.timeOptions.count >= 3 {
cell.thirdTimeOption.setTitle(oneResponse.timeOptions[2], forState: .Normal)
cell.thirdTimeOption.hidden = false
} else {
cell.thirdTimeOption.hidden = true
}

或者,如果您像我一样讨厌看到这样重复的代码,您可以枚举这三个按钮的数组:

for (index, button) in [cell.firstTimeOption, cell.secondTimeOption, cell.thirdTimeOption].enumerate() {
if oneResponse.timeOptions.count > index {
button.setTitle(oneResponse.timeOptions[index], forState: .Normal)
button.hidden = false
} else {
button.hidden = true
}
}

理论上您可以使用 outlet 集合来达到相同的目的,但我不会在必须遵守顺序的地方使用 outlet 集合。

关于ios - 为什么数据没有填充到我的自定义 ui TableView 单元格中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34026142/

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