单元格不会显示在UICollectionView中

时间:2018-11-08 18:16:19

标签: ios swift uicollectionview

我正在尝试创建UICollectionView并在其中显示一些单元格。

这是我的代码:

    class MainVC: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {

        var mForecast = [CustomCollectionViewCell]()

        let CVCellIdentifier = "forecastCell"

        lazy var mCollectionView: UICollectionView = {

            var collectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: 300, height: 150), collectionViewLayout: UICollectionViewFlowLayout())
            collectionView.clipsToBounds = true
            collectionView.backgroundColor = .red

            collectionView.translatesAutoresizingMaskIntoConstraints = false
            return collectionView

        }()

        override func viewDidLoad() {

            super.viewDidLoad()
            view.backgroundColor = UIColor(red: 80/255, green: 135/255, blue: 179/255, alpha: 1.0)

            setupNavBar()
            self.navigationItem.searchController = mSearchBarController

            setupMainWeatherIcon()
            fillArrayWithData()
            mCollectionView.register(CustomCollectionViewCell.self, forCellWithReuseIdentifier: CVCellIdentifier)
            mCollectionView.dataSource = self
            mCollectionView.delegate = self

        }

    private func fillArrayWithData(){
        for _ in 1...6 {
            let forecastCell: ForecastCell = ForecastCell()

            forecastCell.mDayLabel = "DAY-TEST"
            forecastCell.mWeatherIcon = UIImage(named: "partly-cloudy")
            forecastCell.mTempLabel = "TEMP-TEST"

            mForecast.append(forecastCell)
        }

        mCollectionView.reloadData()
    }
        //MARK: COLLECTION VIEW METHODS
        func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

            return mForecast.count

        }

        func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
            let cell = mCollectionView.dequeueReusableCell(withReuseIdentifier: CVCellIdentifier, for: indexPath) as! CustomCollectionViewCell

            return cell
        }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: (view.frame.width / 6) - 16 , height: 70)

    }

    func collectionView(_ collectionView: UICollectionView, layout              
      collectionViewLayout: UICollectionViewLayout, insetForSectionAt  
      section: Int) -> UIEdgeInsets {
        return UIEdgeInsets(top: 10, left: 8, bottom: 10, right: 8)
      }
    }

这是CustomCollectionViewCell类:

import UIKit

class CustomCollectionViewCell: UICollectionViewCell {
    var mDayLabel: String?
    var mWeatherIcon: UIImage?
    var mTempLabel: String?

let dayTV: UILabel = {

    var label = UILabel()
    label.textAlignment = .center
    label.font = UIFont.boldSystemFont(ofSize: 12)
    label.textColor = .blue

    label.translatesAutoresizingMaskIntoConstraints = false
    return label

}()

let weatherImg: UIImageView = {

    var img = UIImageView()

    img.translatesAutoresizingMaskIntoConstraints = false
    return img

}()

let tempLabel: UILabel = {

    var label = UILabel()
    label.textAlignment = .center
    label.font = UIFont.systemFont(ofSize: 8)
    label.textColor = .blue

    label.translatesAutoresizingMaskIntoConstraints = false
    return label

}()

override init(frame: CGRect) {
    super.init(frame: frame)
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

override func layoutSubviews() {
    super.layoutSubviews()

    if let label = mDayLabel{
        dayTV.text = label
    }
    if let image = mWeatherIcon{
        weatherImg.image = image
    }
    if let temp = mTempLabel{
        tempLabel.text = temp
    }

    setupDayTextView()
    setupWeatherImage()
    setupTempLabel()

}

private func setupDayTextView(){

    addSubview(dayTV)

    dayTV.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
    dayTV.topAnchor.constraint(equalTo: self.topAnchor, constant: 10).isActive = true

}

private func setupWeatherImage(){

    addSubview(weatherImg)

    weatherImg.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
    weatherImg.topAnchor.constraint(equalTo: dayTV.bottomAnchor, constant: 10).isActive = true

}

private func setupTempLabel(){

    addSubview(tempLabel)

    tempLabel.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
    tempLabel.topAnchor.constraint(equalTo: weatherImg.bottomAnchor, constant: 10).isActive = true

}
}

当然,我有一种用数据填充mForecast数组的方法,如果我添加一个子视图,我确实会看到该单元格,但是我知道我不应该这样做来查看集合视图中的单元格。 / p>

我尝试查找,但是找不到此处的问题以及为什么无法显示单元格。 这就是我得到的

enter image description here

1 个答案:

答案 0 :(得分:0)

原始:

设置委托和数据源后,您需要调用collectionView.reloadData()

修订:

您正在呼叫fillArrayWithData,在完成配置collectionView的数据源和委托之前,它会呼叫reloadData。因此,在调用reloadData时,没有源可以设置数据并加载单元格。

完成集合视图的配置后,尝试调用fillArrayWithData

我个人建议在viewDidLoad或collectionView的didSet属性观察器中配置您的收藏夹视图:

var collectionView: UICollectionView! {
     didSet {
         collectionView.delegate = self
         collectionView.dataSource = self
     }
}

然后我用viewWillAppear方法启动数据加载。

示例:

override func viewDidLoad() {

        super.viewDidLoad()
        view.backgroundColor = UIColor(red: 80/255, green: 135/255, blue: 179/255, alpha: 1.0)

        setupNavBar()
        self.navigationItem.searchController = mSearchBarController

        setupMainWeatherIcon()

        // This is where you are calling fillArrayWithData right now.
        // fillArrayWithData()

        mCollectionView.register(CustomCollectionViewCell.self, forCellWithReuseIdentifier: CVCellIdentifier)
        mCollectionView.dataSource = self
        mCollectionView.delegate = self

        // This is where you should be calling fillArrayWithData
        fillArrayWithData()

    }