我有兩個問題。uicollectionview - 無需調用reloaddata自動加載數據
我很奇怪,爲什麼我收集的看法自動加載數據,而無需調用
imageCollectionView.reloadData()
。已解決。見評論
爲什麼
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize
不叫?我沒有看到print("collectionViewLayout called")
我試圖修改單元格的大小,因此單元格的高度等於集合視圖高度已解決。見註釋
這裏是代碼
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!
}
感謝您的幫助。
'FUNC的CollectionView(的CollectionView:UICollectionView,佈局collectionViewLayout:UICollectionViewLayout,sizeForItemAtIndexPath indexPath:NSIndexPath) - > CGSize'是從一個在doc不同:'FUNC的CollectionView(_的CollectionView:UICollectionView,佈局collectionViewLayout:UICollectionViewLayout,sizeForItemAt indexPath :IndexPath) - > CGSize'(它缺少像其他的'_'。 – Larme
CollectionView首次自動加載,'viewDidLoad'後,我希望你知道:) –
哦,謝謝你。所有的問題解決。 –