Home > Mobile >  Collection View not showing cell
Collection View not showing cell

Time:03-08

I've been trying to create a collection view for days. I have already created several Collection Views. That worked without any problems. Only this time I can't find the error.

The problem: I have an array of Image URLs. These should be displayed in an image view. The Collection View cell is not displayed. The queries "print(CollectionView.visibleCells) always returns 0. However, the array contains elements.

Here my code.

import UIKit

class FreierAuftragAnlegenVC: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    
    @IBOutlet weak var cvFotos: UICollectionView!
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return aktuellerFreierAuftrag.fotoURLs.count
        
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = cvFotos.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! freierAuftragAnlegenCVC
        
       let url = URL(string: aktuellerFreierAuftrag.fotoURLs[indexPath.row])
        
        if let data = try? Data(contentsOf: url!) {
            if UIImage(data: data) != nil {
                cell.ivFoto.image = UIImage(data: data)
            } else {
                print("kein Foto hinter der URL")
            }
        }
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
         return CGSize (width: 100, height: 200)
     }

    @IBAction func btTEst(_ sender: Any) {
        cvFotos.reloadData()
        print(aktuellerFreierAuftrag.fotoURLs.count)
        print(cvFotos.visibleCells)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        cvFotos.dataSource = self
        cvFotos.delegate = self
        cvFotos.collectionViewLayout = UICollectionViewLayout()
}
    
}

import UIKit

class freierAuftragAnlegenCVC: UICollectionViewCell {
    
    @IBOutlet weak var ivFoto: UIImageView!
    
}

enter image description here

enter image description here

Where could the problem be?

CodePudding user response:

Dear you are inheriting from UICollectionViewDelegateFlowLayout only. please add UICollectionViewDataSource, UICollectionViewDelegate as well

i think you are using UIPickerViewDataSource, UIPickerViewDelegate by mistake.

  • Related