【问题标题】:Cycle Through Array To Populate Collection View循环遍历数组以填充集合视图
【发布时间】:2018-07-19 17:10:11
【问题描述】:

我目前正在制作一个天气应用程序,并希望在集合视图中填充当天的天气。数据设置在一个数组中,如下所示:

[["2018-07-19 18:07951670925:07951670925", "cloudy2", "22"], ["2018-07-19 21:00:00", "cloudy2", "18"], ["2018-07-20 00:00:00", "cloudy2", "15"], ["2018-07-20 03:00:00", "cloudy2", "13"], ["2018- 07-20 06:00:00", "cloudy2", "16"], ["2018-07-20 09:00:00", "cloudy2", "21"]]

对于我的集合视图单元格,我有一个填充 2 个标签和一个图像视图的函数

cell.dispayContent(time: string, image: UIImage(named: string), temperature: string)

我需要一种循环遍历数组并将相关信息添加到单元格中的方法。我试过这个:

cell.dispayContent(time: todayModel.todayArray[indexPath][0], image: UIImage(named: todayModel.todayArray[indexPath][1])!, temperature: todayModel.todayArray[indexPath][2])

但是得到错误'Cannot subscript a value of type '[[String]]' with a index of type 'IndexPath'

任何帮助将不胜感激:)

【问题讨论】:

  • indexPath.row 而不是indexPath 我猜(或indexPath.section,但这很少见)?但很明显,我不会使用String 的数组。我会使用自定义结构/类。这更有意义。

标签: arrays swift uicollectionview


【解决方案1】:

您应该使用indexPath.row 而不是IndexPath,如下所示,

let today = todayModel.todayArray[indexPath.row]
cell.dispayContent(time: today[0], image: UIImage(named: today[1])!, temperature: today[2])

【讨论】:

    猜你喜欢
    • 2022-01-06
    • 2020-05-30
    • 2021-09-12
    • 2022-01-27
    • 1970-01-01
    • 2020-09-10
    • 2018-03-07
    • 1970-01-01
    • 2021-05-21
    相关资源
    最近更新 更多