在我滚动之前,UICollectionView没有完全加载

时间:2016-10-12 20:53:18

标签: ios swift cocoa-touch uicollectionview uicollectionviewcell

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

Before scrolling 在滚动之前

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 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:2)

我通过在cell.layoutIfNeeded()之前添加return cell来解决问题。所有内容都按预期加载而不进行任何滚动!