不同的单元格取决于单元格文本



我有以下情况。我正在制作一个天气应用程序,并在单元格中显示一段时间的数据。我有以下结构:

第1天

最低温度

最高温度

第2天

最低温度

最高温度

第3天

最低温度

最高温度

等等…

我也在使用部分和以下功能:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: "minimumTemperature")
cell?.textLabel?.text = "fooMinTemperature"
return cell!
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 2
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 10
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return  // logic for getting the heading of the section
}

问题来了,override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell

函数一次只能返回一个单元格。例如,这个函数现在会被调用两次,并将最低温度加两次。如何添加最低和最高温度。

假设您有一个数据数组,其中数组中的每个元素代表一天,那么您将使用该部分来选择正确日期的数据。然后使用该行在最低或最高温度之间进行选择。

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: "minimumTemperature") as! UITableViewCell
let data = myArrayOfDays[indexPath.section]
let temp = indexPath.row == 0 ? data.minimumTemp : data.maximumTemp
cell.textLabel?.text = "(temp)"
return cell
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 2
}
override func numberOfSections(in tableView: UITableView) -> Int {
return myArrayOfDays.count
}

我使用的变量名称显然只是示例。根据需要更新您自己的。

您的章节标题可以显示日期。

相关内容

最新更新