我正在制作一个简单的天气应用程序,我的表格 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/