【问题标题】:uicollection view within a tableview cell getting ERROR: Index out of range, why?表格视图单元格中的 uicollectionview 出现错误:索引超出范围,为什么?
【发布时间】:2018-05-29 05:45:43
【问题描述】:

我有一个 Venue 类型的数组(4 项),每个数组都包含一个 imgURLs 字符串的数组(2 项),我在下面收到 ERROR: Index out of range我的collectionViewcellForItemAt 的行。我正在尝试在 tableview 单元格内的 collectionView 中显示每个 Venue 的图像。

let urlString = filteredVenueArray[collectionView.tag].imgURLs[collectionView.tag]

cellForItemAt函数:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "VenueListingCellImage", for: indexPath) as! VenueListingCellImage

    let urlString = filteredVenueArray[collectionView.tag].imgURLs[collectionView.tag]

    let imgURL = URL(string: urlString)

    cell.configureCell(url: imgURL!)

    return cell
}

编辑:numberOfItemsInSection 函数

 func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        return filteredVenueArray[collectionView.tag].imgURLs.count

    }

【问题讨论】:

  • 显示 numberOfItemsInSection 方法代码
  • 我已经编辑了我的问题以包含 numberOfItemsInSection 函数。

标签: ios swift uicollectionview


【解决方案1】:

改变这一行

let urlString = filteredVenueArray[collectionView.tag].imgURLs[collectionView.tag]

let urlString = filteredVenueArray[collectionView.tag].imgURLs[indexPath.item]

【讨论】:

  • 这已经奏效了,但这产生了一个问题,即当我选择一个地点来查看详细信息并转回桌面视图时,它被重新加载并且图像现在不与地点对齐?跨度>
【解决方案2】:

在您的代码中替换:

  let urlString = filteredVenueArray[collectionView.tag].imgURLs[collectionView.tag]

与:

  let urlString = filteredVenueArray[collectionView.tag].imgURLs[indexPath.item]

【讨论】:

    【解决方案3】:

    CollectionView cellForItemAt indexPath 通过indexPath 迭代您的数组,因此要访问每个项目,您应该使用indexPath.item

    这是问题,你应该使用,

    let urlString = filteredVenueArray[collectionView.tag].imgURLs[indexPath.item]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-02
      • 1970-01-01
      • 1970-01-01
      • 2016-08-22
      • 1970-01-01
      • 2015-09-12
      • 2014-04-10
      相关资源
      最近更新 更多