我从服务器获取数据,我想把它放在我的自定义UITableViewCell
中这是故事板中的单元格
如你所见,有两件事:
当我从服务器收到数据时,我这样做:
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
}
如你所见,我正在追逐标签和按钮,
但是,按钮没有被更改,请查看结果:
我试图在代码中看到打印,打印的数据是正确的,请查看
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"]
为什么在单元格中不正确?
抱歉,我的英语不好答案 0 :(得分:3)
设置按钮标题时,应使用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)
}
顺便说一句,因为你正在重用单元格,你真的应该检查这些else
语句的if
子句,如果它失败则隐藏按钮:
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
}
}
理论上你可以在同一端使用插座系列,但我不会使用必须遵守订单的插座系列。
答案 1 :(得分:0)