0

我試圖從.xib加載UICollectionViewCell到UICollectionView,UICollectionView也是在.xib中可重複使用的UIView。這個UIView將被加載到更多的UIViewControllers。我發現,我需要這樣的稱重傳感器:加載UIVollectionViewCell到UIView

[self.collectionView registerNib:[UINib nibWithNibName:@"MyCell" bundle:nil] forCellWithReuseIdentifier:@"CELL"]; 

但它不會加載它。 我看了所有網絡的答案,但我找不到這個具體的情況。

回答

3

有兩種不同的情況下,

1)如果你使用故事板和定製單元。

2)筆尖和簡單的UIView(UIViews的層次結構)。

你好像是第二種情況, 首先你需要製作一個UICollectionView的實例(通過alloc init方法)。 然後使用以下方法註冊持有您的細胞/視圖的筆尖。

[self.collectionView registerNib:[UINib nibWithNibName:CELL_IDENTIFIER bundle:nil] forCellWithReuseIdentifier:CELL_IDENTIFIER]; 

,然後就可以使用,使用下面的方法

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { 
UICollectionViewCell *cell; 
cell = (UICollectionViewCell*)[collectionView dequeueReusableCellWithReuseIdentifier:CELL_IDENTIFIER 
                     forIndexPath:indexPath]; 
//Populate your data here. 
return cell; 

}

希望這有助於筆尖.. :)