2013-06-23 9 views
1

基本上我一直在寫iOS應用程序大約3天(在閱讀書籍以獲得設計模式和框架等基本知識之後),並花費了整整1天這個。我無法顯示我的CollectionView。我有一個以TabBarView開頭的故事板,兩個選項卡(一個是TableView,另一個是UIView),但第三個選項卡(UICollectionView)即使在設置後也會顯示黑屏。 我如何設置它:在Xcode中遇到與CollectionView有關的問題 - 顯示器的黑屏

1)拖着一個視圖控制器到故事板

2)使我UITabBarController之間的關係segueViewController

3)拖着UICollectionViewViewController

4)拖動4 UICollectionViewCell'sCollectionView

5)dragg ED 4 UILabels成說CollectionViewCell's

6)取得了新的CollectionView's委託&數據源和我CollectionView類的頭文件之間的連接(這可能是我哪裏錯了,我沒有讓我的ViewController.h類的連接因爲我用這一個我UITableView,並認爲一個新的類是必要的)

7)宣佈我CollectionView.m文件中的下列方法

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
//Populates each cell using the data given (no data in this case). 
//Should return a CollectionViewCell. 
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath]; 

return cell; 
} 

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section 
{ 
    //Gets the number of cells in each section. 
    //Should return an integer value. 
    return 2; 
} 

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView 
{ 
    //Tells the collection view the number of sections it should have. 
    //Should return an integer value. 
    return 4; 
} 

我感覺有這麼我錯過了,因爲這個邏輯對我來說是一個程序員,但是,在Xcode中有這麼一點經驗,也許我忘了把這個類連接到我的CollectionView什麼的。有人知道我出錯了嗎?如果你想知道,我使用這個CollectionView更多的導航技術,而不是顯示美妙的圖像或任何東西,我將在稍後他們實際顯示在模擬器中時用通用圖像填充它們。 謝謝

回答

3

它沒有顯示任何內容,因爲標籤的默認顏色是黑色,您必須將文本的顏色更改爲任何淺色以查看單元格上的文本。

Now, 

- >添加這應該是子UICollectionViewCell一個新的文件,按Ctrl +從標籤拖動到這個類(比如我把mainViewCell)和標籤屬性的名字,我花了LabelText的

- >聲明reuseIdentifier從屬性檢查器,電池(mainCell,我花了)

- >導入cellClass您的UIViewController氯屁股,

- >使用這種方法來顯示任何文本

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    mainViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"mainCell" forIndexPath:indexPath]; 
    cell.labelText.text = @"What-ever-you-want-display"; 
    return cell; 
} 

希望這將有助於...

0

你是否告訴你的視圖控制器(包含集合視圖)使用UICollectionViewDelegateFlowLayout?例如,

@interface RootViewController() <UICollectionViewDelegateFlowLayout, UICollectionViewDataSource> 

此外,您是否爲您的單元設置了重用標識符?在Interface Builder中,在屬性檢查器中進行設置。然後轉到cellForItemAtIndexPath:方法並更改此行以使用您的標識符。 UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@「MyCell」forIndexPath:indexPath];

在文檔中查看UICollectionViewDelegateFlowLayout,瞭解用於設置單元格大小和設置邊距和間距的方法。

0

步驟6)可能不足。你是否已經將控件圖標直接拖動到主視圖下的控制器圖標上?您會在黑色對話框中提示連接數據源和委託。這種連接是通過XCode進行的。 或者,您可以在控制器實現中顯式地將dataSource設置爲self。

0

取下默認視圖控制器代碼片段的registerClass代碼

[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];