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

时间:2016-09-08 01:37:57

标签: ios swift tableview

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

      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

1 个答案:

答案 0 :(得分:0)

我认为您将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)
    }

}