Sel UICollectionView tidak terlihat (UICollectionView cells are invisible)


問題描述

Sel UICollectionView tidak terlihat (UICollectionView cells are invisible)

I recently added a UICollectionView to my storyboard, it's currently pushed into view by another view and this appears to be working fine however, using the storyboard editor i set the view to contain 35 cells which in the editor look fine, but when i run the app the cells are invisible. Some of the cells have UIButtons inside and these don't render either.

To double check the view was rendering i changed the background colour in the editor and ran it again, the colour updated correctly. Am i right in assuming that the setup should automatically render the cells without me having to run any delegate code if they have been setup in the editor?

Any help would be appreciated.

Thanks

EDIT‑‑‑‑

Ok i have implemented the appropriate delegate methods in my second view controller that then pushes a segue to bring up the collectionview controller, i also added the  to my second view controller header file and added the following code to the .M file:

// number of sections for the view
‑ (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

// numbers of items in section
‑ (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 1;
}

// cell to return (uses the custom identifier of the cell in the storyboard)
‑ (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"quickNoteCell" forIndexPath:indexPath];
    return cell;
}

I have also made sure the identifier in the cell on the storyboard uses the quickNoteCell, and changed its default colour to blue, however i am still not seeing the cell any ideas?

‑‑‑‑‑

參考解法

方法 1:

You are not seeing any cells because you have generated a UICollectionViewController subclass, which added the following line in viewDidLoad:. From my understanding, this produces blank cells that do get compressed to size 0,0 when using autolayout.

Delete this line to see the cells that you defined in storyboard instead of invisible UICollectionViewCells

 // Register cell classes
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];

方法 2:

Like Jay said, put the line here under your @interface and it should work. Nothing is wrong in the code you posted.

<UICollectionViewDelegate, UICollectionViewDataSource>

方法 3:

Have you implemented the UICollectionViewDelegate and UICollectionViewDataSource protocols?

Is collectionView:cellForItemAtIndexPath: being called?

方法 4:

if you are using custom collectionViewItems and you want to load data from nib file register first nib to collectionView:

let nib = UINib(nibName: reuseIdentifier, bundle: Bundle.main);
self.collectionView?.register(nib, forCellWithReuseIdentifier: reuseIdentifier);

and in collection view delegate just call:

collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath)

(by SmokersCoughAlex StoneAlexJay SlupeskyConstantin Saulenco)

參考文件

  1. UICollectionView cells are invisible (CC BY‑SA 3.0/4.0)

#uicollectionviewcell






相關問題

Sel UICollectionView tidak terlihat (UICollectionView cells are invisible)

UICollectionViewCell裡面的UIWebView不調用Delegate方法 (UIWebView inside UICollectionViewCell does not call Delegate Method)

Bộ sưu tập xem vấn đề thay đổi kích thước iPhone 4 và iPhone 5 (Collection view resizing issue iPhone 4 vs iPhone 5)

集合視圖單元格未出現 (Collection View Cells not appearing)

UICollectionView 單元格大小 ContentView 問題 (UICollectionView cell size ContentView Issue)

在 Swift 中使用 CollectionView 進行網格佈局 (Grid layout with CollectionView in Swift)

如何為collectionviewcell的刪除自定義動畫? (How to custom animation for collectionviewcell's deletion?)

檢測位於另一個 uicollectionview 內的 uicollectionView 的哪個 UICollectionViewCell 在中心 (Detect which UICollectionViewCell of a uicollectionView that is inside another uicollectionview is in the center)

navigationController?.pushViewController 不工作 (navigationController?.pushViewController is not working)

XCode UICollectionViewCell 在模擬器上看起來比在故事板中小得多 (XCode UICollectionViewCell appearing much smaller on simulator than in storyboard)

CollectionViewCell 未出現在 collectionView 中 (CollectionViewCell not appearing in collectionView)

由 LPLinkView 和 UIImageView 組成的單元格的 CollectionView 速度很慢,並且在滾動時會重新加載數據 (CollectionView with Cells made up of LPLinkView's and UIImageView's is Slow and Reloads Data While Scrolling)







留言討論