ios - 数组值未显示在自定义表格单元格中

标签 ios swift tableview

我正在制作一个简单的天气应用程序,我的表格 View 应该通过在左侧显示日期和在右侧显示高温来显示几周的天气预报。我的 cellForRowAtIndexpath 如下。它在我的自定义日期标签中正确显示日期数组,但包含最高温度的最大数组未显示。当我打印出最大数组值时,它看起来是正确的,所有数字都列在那里,但仍然没有显示。如果我只是在该文本标签中硬编码一个值(例如“测试”),那么该值将会显示,但数组不会显示。

      override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

      let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! DailyWeatherTableViewCell

      cell.daylabel.text = dayarray[indexPath.row] as? String

      cell.maxlabel.text = maxarray[indexPath.row] as? String

      return cell

更新:我的数组设置如下:

dayarray = [dateString1,dateString2, dateString3, dateString4, dateString5, dateString6, dateString7]

maxarray = [max1int, max2int, max3int, max4int, max5int, max6int, max7int]

这些值的来源有点复杂,但如果我打印出 maxarray,它看起来像这样:

100
100
92
90
90
89
89

最佳答案

我认为将 Int 转换为 String 的方式会导致该值为零。尝试更改为以下内容:

cell.maxlabel.text = String(maxarray[indexPath.row])

不同的是,String()是根据Int值创建一个字符串对象。而 as? 运算符通常在数组中有不同类型的对象并且希望将其转换为特定类型时使用。例如

let arr = [1, 21, 3, 5, "Hello world", 1, 2, "food"]

for element in arr {
    if let stringValue = element as? String {
        print(stringValue)
    }
    else if let intValue = element as? Int {
        print (intValue)
    }

}

关于ios - 数组值未显示在自定义表格单元格中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39381341/

相关文章:

ios - UIApplicationExitsOnSuspend 关键问题。我如何解决它?

ios - 如何设置图像的色调颜色

ios - 如何更新 TableView 静态单元格中的数据?

ios - 钛焦点事件在 ios 上多次触发

ios - 使用 dequeueReusableCellWithIdentifier 指定单元格时停止影响随机 UITableViewCells

objective-c - 如何传递 JSON 请求

ios - 使用 FBSDKLoginkit 获取用户名和头像

xcode - 如何在 Swift 中表示 NULL 选择器

swift - 如何在 SwiftUI 中删除 TextView 上方和下方的空间?

javascript - 从数据库将行添加到 TableView