Ios 为什么不在自定义ui表视图单元格中填充数据

Ios 为什么不在自定义ui表视图单元格中填充数据,ios,swift,uitableview,Ios,Swift,Uitableview,我正在从服务器获取数据,并希望将其放在自定义UITableViewCell中 这是故事板上的单元 如你所见,有两件事: 首选项标签 三个按钮 当我从服务器接收数据时,我会执行以下操作: override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueRe

我正在从服务器获取数据,并希望将其放在自定义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"]
    
    为什么它在单元格中不正确


    对不起,我的英语不好

    设置按钮标题时,应使用
    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
        }
    }
    

    从理论上讲,您可以将门店集合用于同一目的,但我不会在必须遵守订单的情况下使用门店集合。

    我以前遇到过类似问题

    调试程序时,是否调用该函数?


    如果没有,则需要确保在dataSource委托处设置ViewController。您可以在主情节提要中通过按住ctrl键并单击TableView,然后将其拖动到视图顶部最左侧的黄色圆圈并选择“dataSource”来完成此操作。这将设置table view将调用该类中所有必要的函数。

    这通常是一个很好的提示,但在这种情况下,如果她忽略了连接数据源,
    首选项标签
    就不会成功设置,
    打印
    语句也不会出现在控制台中。Rob是正确的,我已经设置了代理和出口,甚至没有告诉你如果没有文本,我应该移除按钮,你自己也明白。你总是提出最好的解决方案。您不仅是一位专家,您还是swift和ios的所有者,我非常非常感谢您mych。很幸运在这个社区见到你。从现在起,你就是那种抢夺的人,你好,再次请帮帮我国王:)