大家好,溢位者們,所以我設定了一個 collectionView 并對其進行了配置,但是當我運行模擬器時,我得到了很多單元格,就像第一張圖片中那樣相互混合。
我在故事板單元格中添加了識別符號,也為該單元格創建了類,但我似乎不知道問題可能是什么,因為它從未發生過。
我希望我的細胞如何,我將在第二張圖片上展示。
第一張照片
第二張圖片
代碼:
extension dashboardViewController: UICollectionViewDelegate, UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{
return 15
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "dashboardCollectionViewCell", for: indexPath) as? dashboardCollectionViewCell else {return UICollectionViewCell()}
return cell
}
}
uj5u.com熱心網友回復:
通過提供的影像,您似乎沒有設定水平布局中單元格的專案大小以及每個單元格之間的最小間距。您可以通過呼叫來做到這一點(設定專案大小)
func collectionView(
_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath
) -> CGSize {}
這個方法來自UICollectionViewDelegateFlowLayout. 您也可以使用情節提要來做到這一點。
尺寸檢查器
uj5u.com熱心網友回復:
似乎您的集合視圖單元格的大小設定不正確。如果您正在使用UICollectionViewFlowLayout,也許您可??以檢查布局物件的itemSize屬性。
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
...
// Set a suitable CGSize for the `itemSize` property
// In this example I set it to 100pt * 50pt
layout.itemSize = CGSizeMake(100, 50);
...
UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/454396.html
