UICollectionView没有出现

时间:2017-10-24 13:38:07

标签: ios swift

当我构建我的项目(模拟器/设备)时,UICollectionView没有出现。

我设置(我认为是这样)所有代表,功能等。我仍然没有这种观点。

class WeatherViewController: UIViewController, CLLocationManagerDelegate,UICollectionViewDelegate,UICollectionViewDataSource{


    @IBOutlet weak var collectionView: UICollectionView!


    var forecast: Forecast!
    var forecasts = [Forecast]()


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.


        collectionView.delegate = self
        collectionView.dataSource = self
    }

    func numberOfSectionsInCollectionView(collectionView: UICollectionView!) -> Int{
        return 1
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return forecasts.count
    }


    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ForecastCell", for: indexPath) as? ForecastCollectionViewCell {
            let forecast = forecasts[indexPath.row]
            cell.updateForecastCell(forecast: forecast)
            return cell
        } else {
            return ForecastCollectionViewCell()
        }
    }

}

1 个答案:

答案 0 :(得分:1)

我下载了该项目并从dataSource删除了delegatestoryboard,因为您已在代码中设置了它们。

您使用Stack Views。当我将collectionView移出Stack Views时,您的代码按预期工作。

不幸的是,我不知道为什么你不能在collectionView内加Stack View。也许你会找到一个解决方案,然后请发布。

(我的个人意见:我真的不喜欢Stack Views。似乎他们让你的生活更轻松,但我无法应用。) enter image description here