为什么我的自定义 ui 表视图单元格中没有填充数据 [英] why the data is not populated in my custom ui table view cell
问题描述
我从服务器获取数据,我想把它放在我的自定义 UITableViewCell 中
I am getting data from the server and i want to put it in my custom UITableViewCell
这是故事板中的单元格
如您所见,有两件事:
- 偏好标签
- 三个按钮
当我从服务器接收数据时,我这样做:
when i receive the data from the server, i do this:
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
}
如你所见,我正在链接标签和按钮,
as you see, i am chaing the label and the buttons,
然而,按钮并没有被改变,请看结果:
however, the buttons aren't being changed, please look at the result:
我试着把打印放在代码中,打印出来的数据是正确的,请看
i tried to put print as you see in the code, and the data printed is correct, look please
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"]
为什么它在单元格中不正确?
why it is not being correct in the cell?
对不起,我的英语不好
推荐答案
设置按钮标题时,应使用setTitle(forState:)
.例如:
When setting a button title, you should use setTitle(forState:)
. For example:
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
子句,如果失败就隐藏按钮:
As an aside, because you are reusing cells, you really should check for the else
clause for these if
statements, hiding the button if it fails:
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
}
<小时>
或者,如果您像我一样讨厌看到这样重复的代码,您可以枚举这三个按钮的数组:
Or, if you're like me and hate seeing code repeated like that, you could enumerate through an array of these three buttons:
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
}
}
理论上,您可以将出口集合用于同一目的,但我不会使用必须遵守顺序的出口集合.
Theoretically you could use an outlet collection to the same end, but I don't use outlet collections where order must be honored.
这篇关于为什么我的自定义 ui 表视图单元格中没有填充数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!