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

标签 ios swift uitableview

我正在从服务器获取数据,我想把它放在我的自定义 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/

相关文章:

ios - 检查应用程序是否由于 RemoteNotification 而变得事件(应用程序已启动)

iphone - 如何突出显示 UITableView 中的一行

ios - 我如何在 Swift 中设置我的 tableview 的 Action 按钮垂直对齐?

iphone - 以相同的名称将 iPhone 和 iPad 应用程序提交到商店

ios - PromiseKit 和 CloudKit 保存(使用 Swift)

macos - NSSplitViewController subview 未添加

ios - 滚动完成时居中单元格 UITableView

iphone - 点击 uitableviewcell iOS 后加载 uiviewcontroller

ios - 将 NSData 转换为 NSString 返回随机字符

iphone - 不可编辑的 UITextView 的双击事件