Search code examples
iosswiftuicollectionviewuicollectionviewcellreloaddata

uicollectionview - data automatically load without calling reloaddata


I got two questions.

  1. I am wondering why my collection view automatically load data without calling imageCollectionView.reloadData().

    Solved. See comment

  2. Why func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize is not called? I didn't see the print("collectionViewLayout called") I am trying to modify the cell's size so the cell's height equals to the collection view height

    Solved. See comment

Here is the codes

class ProductInternalDetailVC: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {

    var selectedProduct: Product?
    @IBOutlet weak var imageCollectionView: UICollectionView!

    override func viewDidLoad() {
        super.viewDidLoad()
        imageCollectionView.delegate = self
        imageCollectionView.dataSource = self
    }

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

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath as IndexPath) as! ProductInternalDetailCVC
        if indexPath.row == 0 {
            cell.productImage.image = selectedProduct!.image1
        } else {
            cell.productImage.image = selectedProduct!.image2
        }
        cell.productImage.frame = CGRect(x: 1, y: 1, width: cell.frame.size.width-2, height: cell.frame.size.height-2)
        return cell
    }

    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
        //return CGSize(width: collectionView.frame.size.height-1, height: collectionView.frame.size.height-1)
        print("collectionViewLayout called")
        return CGSize(width: 10, height: 10)
    }
}

class ProductInternalDetailCVC: UICollectionViewCell {
    @IBOutlet weak var productImage: UIImageView!
}

Thanks for the help.


Solution

  • Question 2: The function signature has changed a bit. Change the function signature with this:

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        ... 
    }
    

    And don't forget to implement the UICollectionViewDelegateFlowLayout protocol.

    Question 1:

    When entering the ViewController your collectionView loads the datasource of the collectionView automatically if this is your question.for example, make changes on datasource without changing the view (means without calling viewDidLoad method) you will not see the changes until you call imageCollectionView.reloadData().