【问题标题】:UICollectionView not loading fully until I scrollUICollectionView 在我滚动之前没有完全加载
【发布时间】:2017-02-21 19:41:15
【问题描述】:

我有一个集合视图,我想在其中显示每小时天气。我似乎在加载单元格时遇到问题,并且由于某种原因向前滚动然后向后滚动完全加载单元格。在我滚动集合视图之前,所有约束都不起作用,并且一个标签没有显示它的信息。

滚动前

滚动后(这就是我希望单元格的样子)

func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}


func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of items
    return newhourlyWeather.count
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! hourlyWeatherCell

    // Configure the cell

    let hWeather = newhourlyWeather[indexPath.row]

    if let HourlyTemp = hWeather.temperatureh {
        cell.temperatureHLabel.text = "\(HourlyTemp)º"
    }

    if let HourlyTime = hWeather.convertedTimeH {
        cell.timeHLabel.text = "\(HourlyTime)"
    }

    if let HourlyRain = hWeather.precipProbabilityh {
        cell.rainChanceHLabel.text = "\(HourlyRain)%"
    }

     cell.iconhView.image = hWeather.iconh

    return cell

    self.collectionView.reloadData()
}

【问题讨论】:

  • 知道什么问题我可能需要一些代码
  • 没有代码和约束的进一步信息,这个问题无法回答。
  • 更新问题@vikingosegundo
  • 重新加载集合视图将填充单元格对我来说似乎不是一个好主意。
  • 但是嘿:你是在返回后执行的,所以它永远不会被执行。

标签: ios swift cocoa-touch uicollectionview uicollectionviewcell


【解决方案1】:

我遇到了同样的问题,我解决了在 collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) 内调用 cell.layoutIfNeeded() 的问题。

此外,如果您使用 UICollectionViewDiffableDataSource 并且在 snapshot 内应用 viewWillAppear,则需要添加一些延迟以使其正常工作,如下所示:

DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
    // apply here the snapshot
}

【讨论】:

    【解决方案2】:

    我通过在return cell 之前添加cell.layoutIfNeeded() 解决了这个问题。一切都按预期加载,无需任何滚动!

    【讨论】:

    • 我认为您遇到了约束问题。
    【解决方案3】:

    似乎您是异步填充单元格的,如果是这样,那么在最后添加一个 mycollectionview.reloadData()。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-10-13
      • 1970-01-01
      • 2020-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多