2013-04-05 20 views
16

我創建了一個對Apple收集視圖示例項目非常簡單的集合視圖。我在故事板的視圖控制器中有一個集合視圖,並在集合視圖右上角的集合視圖單元格內設置了一個標籤。我已經將它與我的自定義單元格中的IBOutlet連接起來了。下面是相關的代碼:自定義UICollectionViewCell中的UILabel始終爲空,無法更新文本

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [self.workoutView registerClass:[Cell class] forCellWithReuseIdentifier:@"Cell"]; 
    ... 
} 

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { 
    if (collectionView == self.collView) { 
     Cell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];  
     cell.segmentTitle.text = @"some text"; 
     cell.backgroundColor = [UIColor whiteColor]; 
     return cell; 
    } 
    return nil; 
} 

我把一個斷點segmentTitle.text部分後segmentTitle總是空。相應地,我在模擬器中看到的是空白框。我錯過了什麼?

回答

41

StoryBoard裏面的UICollectionViewCell不需要registerClass,只需在StoryBoard中選擇reuseidentifier即可。刪除這一行:

// [self.workoutView registerClass:[Cell class] forCellWithReuseIdentifier:@"Cell"]; 

並確保您連接正確方法:在故事板

- 選擇類類型UICollectionViewCell與細胞

-Drag的UILabel進入細胞,並掛接到Cell.h

-Type重用標識符

+5

刪除註冊行並使用故事板中的重用標識符修復它。謝謝! – brodney 2013-04-05 01:31:06

+2

這工作。有時故事板上的魔法太多了。 – 2014-08-08 10:52:46

+0

對不起,你的意思是 - 鍵入重用標識符?我刪除了第一行,但我什麼都看不到(其他步驟) – Signo 2015-02-11 11:27:51

2
MainFeedCollectionView.registerClass(CollectionViewCell.self, forCellWithReuseIdentifier: "CollectionViewCell") 

我除去d從我的代碼中的這一行現在它的工作正常...  

相關問題