2014-01-17 184 views
15

我試圖實現的是一個3x3表格,其中包含UICollectionView。但是它在運行應用程序時不會顯示任何單元格。它確實顯示了CollectionView的背景顏色(我設置爲藍色)有人可以幫我嗎?UICollectionView - 不顯示單元格

我所做的是創建一個storyboard,並簡單地將CollectionViewController拖到它上面。這個控制器也帶了一個嵌套的CollectionView。然後,我簡單地使用設置來使其背景變成藍色,並添加一個單元格,使其背景變爲紫色(兩者都僅用於測試)。最後將一個UILabel拖到它上面。現在運行時只顯示藍色背景,但沒有單元格,即使它們在編輯器中清晰可見。然後,我嘗試創建一個自定義UICollectionViewController,將類指定給CollectionViewController而不是標準類,並對單元執行相同操作(指定自定義UICollectionViewCell)。在自定義UICollectionViewController我實現下面的方法:

-(NSIntegear)numberOfSectionsInCollectionView:(UICollectionView *)collectionView 
{ 
    return 1; 
} 

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section 
{ 
    return 9; 
} 

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *cellIdentifier = @"Cell"; 
    AboutYourGoalsMultiSelectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath]; 
    [[cell goal]setText:@"T"]; 

    return cell; 
} 

我所述小區設置到「小區」的標識符和由數據源和代表的的CollectionView和定製UICollectionViewController之間的出口連接。一切都很好。 那麼我錯過了什麼?在簡單地使用編輯器進行所有這些調整之前,它不應該顯示某種方式嗎?

The storyboard editor with all the changes I did

+0

是用於CollectionViewCell小區標識符設置爲 「小區」? – mharper

+0

是的,集合可重用視圖>標識符被設置爲「單元格」和標識>恢復ID以及 –

+0

沒有什麼突然出現在我身上。我會在'collectionView:cellForItemAtIndexPath:'中設置一個斷點,以確保它被調用,並且b)看看還有什麼可能會出錯的。 – mharper

回答

2

因爲我沒有找到直接的解決它,我試圖建立一個新的項目,也做了同樣的變化和調整,並出人意料的是,這一次,一切正常從一開始就。然後我將故事板複製到我的舊項目中,刪除所有舊文件,並用新項目中的文件替換它。到目前爲止,一切都很好。我不知道實際的錯誤是什麼,但我很高興現在正在工作。感謝大家看看我的問題!

2

你在你的代碼的第一行中的錯誤。

-(NSIntegear)numberOfSectionsInCollectionView:(UICollectionView *)collectionView 

NSIntegear應該NSInteger

不知道是不是這個問題,因爲xcode不應該讓你運行這個錯誤的項目。我只是試着按照你提到的完全相同的步驟(沒有標籤),但採用了上述修正,對我來說工作得很好。下面是截圖:

enter image description here

+0

這只是一個錯字。 – kelin

5

錯誤的一個常見原因是數據源和委託未在

+0

因爲我現在不得不學習,另一個非常令人沮喪的錯誤來源是AutoLayout:確保UICollectionView不會被壓縮到零大小。 – BlenderBender

42

See my answer here,總之,你需要刪除此行,如果您使用的是故事板工作奧特萊斯設置:

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

這是它不適合我的主要原因。使用故事板時,XCode默認插入此項。非常棒。 – qiksand

+0

helpfull aswer ... – Bangalore

+0

只需加入 '[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier]; ' 用於以編程方式註冊由Apple記錄的單元格 [使用registerClass:forCellWithReuseIdentifier:或registerNib:forCellWithReuseIdentifier:方法將您的單元格與重用標識符相關聯。您可以將這些方法作爲父視圖控制器初始化過程的一部分調用。](https://developer.apple.com/library/ios/documentation/WindowsViews/Conceptual/CollectionViewPGforIOS/CreatingCellsandViews/CreatingCellsandViews.html) –

0

如果您嘗試創建一個簡單的CollectionViewController(不是自定義的,即通過實現),並且您的單元格在應用程序運行時不顯示,請按照以下步驟操作: -

  • 首先,您必須爲您的UICollectionViewController創建可可觸摸類(如果未創建)。 FILE - > NEW - > COCOA TOUCH CLASS然後從子類的下拉列表中選擇UICollectionViewController。
  • 現在在您的CollectionViewController的身份檢查器部分下,通過從自定義類的下拉列表中進行選擇來添加您的類。
  • 現在打開你創建的類和編輯你的方法是這樣的:

    override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int { 
    //#warning Incomplete method implementation -- Return the number of sections 
    return 1 
    } 
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    //#warning Incomplete method implementation -- Return the number of items in the section 
    return 1 
    } 
    
  • 只能編輯「回報」值(默認爲0),只是簡單的寫你想要顯示的細胞/項目數或者你已經創建了(在這種情況下,我只創建了一個Cell)。

[IOS] [swift3] [xcode中]

相關問題