gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 11:43:21 26 4
gpt4 key购买 nike

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

第一天

最低温度

最高温度

第二天

最低温度

最高温度

第三天

最低温度

最高温度

等等等等......

我还使用了部分和以下函数:

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

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

最佳答案

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

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
}

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

您的部分标题可以显示日期。

关于ios - 不同的单元格取决于单元格文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53106495/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com