UICollectionView not loading fully until I scroll

2019-08-04 11:16发布

I have a collection view that I want to display hourly weather in. I seem to have a problem with loading the cell, and for some reason scrolling forwards and then back loads the cell fully. Before I scroll the collection view, all of the constraints do not work and one label doesn't show it's info.

Before scrolling Before scrolling

After scrolling (this is how I want the cells to look like) After scrolling (this is how I want the cells to look like)

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()
}

2条回答
小情绪 Triste *
2楼-- · 2019-08-04 11:37

Seems like you populate your cells asynchronously, if so then add a mycollectionview.reloadData() at the end.

查看更多
爷的心禁止访问
3楼-- · 2019-08-04 11:52

I fixed the problem by adding cell.layoutIfNeeded() before the return cell. Everything loaded as expected without any scrolling!

查看更多
登录 后发表回答