幾天來,我一直在嘗試創建一個集合視圖。我已經創建了幾個集合視圖。這沒有任何問題。只是這次我找不到錯誤。
問題:我有一組影像 URL。這些應該顯示在影像視圖中。不顯示集合視圖單元格。查詢“print(CollectionView.visibleCells) 始終回傳 0。但是,陣列包含元素。
這是我的代碼。
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!
}


問題可能出在哪里?
uj5u.com熱心網友回復:
親愛的,您僅從 UICollectionViewDelegateFlowLayout 繼承。請同時添加 UICollectionViewDataSource、UICollectionViewDelegate
我認為您錯誤地使用了 UIPickerViewDataSource、UIPickerViewDelegate。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/439664.html
