2013-07-17 92 views
0

我有一個UICollectionView,顯示許多UICollectionViewCells,我將它們分類爲CardCell。我在- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath中將變量「type」傳遞給CardCell我希望CardCell類能夠根據傳入的類型加載不同的Nib文件。不同的類型需要具有不同的佈局。根據數據更改UICollectionViewCell內容和筆尖佈局

問題是我無法弄清楚在我的CardCell.m中改變這個位置。我嘗試使用- (void)prepareForReuse,但除非用戶正在滾動,否則不會調用。

+0

你不知道在哪裏改變什麼? – rdelmar

+0

更改爲單元格加載哪個nib文件 – tanner0101

+0

您是否嘗試過使用不同的單元格標識符?你可以爲不同的標識符註冊不同的nib,然後從'cellForItemAtIndexPath'出列正確的單元格。 – architectpianist

回答

1

你應該註冊您在viewDidLoad中需要每個筆尖文件,像這樣(的筆尖文件和標識用正確的名稱):

[self.collectionView registerNib:[UINib nibWithNibName:@"RDCell" bundle:nil] forCellWithReuseIdentifier:@"FirstType"]; 

然後,在itemForRowAtIndexPath,試作型,並返回正確的細胞類型:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { 
     if (type = @"firstType") { 
      FirstCell *cell = (FirstCell *) [collectionView dequeueReusableCellWithReuseIdentifier:@"FirstType" forIndexPath:indexPath]; 
      return cell; 
     }else{ 
      SecondCell *cell = (SecondCell *) [collectionView dequeueReusableCellWithReuseIdentifier:@"SecondType" forIndexPath:indexPath]; 
      cell.whatever ..... 
      return cell; 
     } 
} 
+0

太棒了!謝謝 – tanner0101

+0

你有一個範圍問題在這裏。單元格不在if語句之外定義,因此當您返回任何if/else語句之外時單元格不會被定義。 –

+0

@JeremyHicks,當你註冊一個筆尖時,dequeueReusableCellWithReuseIdentifier:forIndexPath:保證返回一個單元格,所以不需要傳統的if(cell == nil)子句。 – rdelmar