2013-10-29 16 views
3

我有一個問題,我不能現在幾個小時解決...UICollectionView中的CollectionView numberOfItemsInSection不承認:

我有不同數量的細胞和不同cellsizes的多個UICollectionViews。 CollectionViews以編程方式創建,並設置代理和數據源。

的collectionviews創建這樣的:

UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; 
[layout setScrollDirection:UICollectionViewScrollDirectionHorizontal]; 
collectionViewOne = [[UICollectionView alloc] initWithFrame:CGRectMake(0,0,320,150) collectionViewLayout:layout]; 
[collectionViewOne setTag:99]; 
[collectionViewOne setDataSource:self]; 
[collectionViewOne setDelegate:self]; 
[collectionViewOne registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"]; 
[collectionViewOne setBackgroundColor:bgColor]; 
[collectionViewOne setShowsHorizontalScrollIndicator:NO]; 
[collectionViewOne setBounces:YES]; 
[collectionViewOne setAlwaysBounceHorizontal:YES]; 
[collectionViewOne setScrollEnabled:YES]; 
[collectionViewOne setRestorationIdentifier:@"collectionViewOne"]; 
[scrollView addSubview:collectionViewOne]; 

我的功能是這樣的:

- (NSInteger)collectionView:(UICollectionView*)collectionView numberOfItemsInSection:(NSInteger)section { 

    NSLog(@"restorationIdentifier: %@", collectionView.restorationIdentifier); 

    if (collectionView == collectionViewOne) 
    { 
     return 3; 
    } 
    else if (collectionView == collectionViewTwo) 
    { 
     return 4; 
    } 
    else 
    { 
     return 1; 
    } 
} 

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { 

    NSLog(@"restorationIdentifier: %@", collectionView.restorationIdentifier); 

    if (collectionView == collectionViewOne) 
    { 
     return CGSizeMake(100, 150); 
    } 
    else if (collectionView == collectionViewTwo) 
    { 
     return CGSizeMake(200, 150); 
    } 
    else 
    { 
     return CGSizeMake(200, 150); 
    } 
} 

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { 

    NSLog(@"restorationIdentifier: %@", collectionView.restorationIdentifier); 

    if (collectionView == collectionViewOne) 
    { 
     UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; 
     imageView.image = [UIImage imageNamed:@"image.png"]; 
     [cell addSubview:imageView]; 
    } 
    else if (collectionView == collectionViewTwo) 
    { 
     //create cell of type 2 
    } 
    return cell; 
} 

在我的日誌我得到下面的輸出(例如):

restorationIdentifier in numberOfItemsInSection:(null) restoreIdentifier in sizeForItemAtIndexPath:(null) restorationIdentifier cellForItemAtIndexPath:collectionViewOne

collectionViewOne是collectionViewOne上的restoreIdentifier。那麼爲什麼前兩種方法不能識別呢?

Ofcourse結果是我得到崩潰,因爲不同CollectionView中的單元格數量和單元格數量沒有正確設置。錯誤:

*** Assertion failure in -[UICollectionViewData validateLayoutInRect:], /SourceCache/UIKit_Sim/UIKit-2903.2/UICollectionViewData.m:341 
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UICollectionView recieved layout attributes for a cell with an index path that does not exist: <NSIndexPath: 0x8c31c10> {length = 2, path = 0 - 2}' 

我該如何解決這個問題?

+0

如何以及在哪裏設置'collectionViewOne'和'collectionViewTwo'?你究竟得到了什麼樣的崩潰? – ChrisH

+0

我將它們設置在我在viewDidLoad中調用的方法中。我得到的崩潰就像斷言失敗 - [UICollectionViewData validateLayoutInRect – Mathijs

+0

很難說沒有更多的代碼和確切的崩潰是什麼錯,但我沒有看到任何處理收集視圖不是一個'cellForItemAtIndexPath'方法或兩個。你確認'collectionViewOne'和'collectionViewTwo'在這些方法中不是零嗎? – ChrisH

回答

10

這已解決。問題在於我爲兩個UICollectionView使用了相同的UICollectionViewFlowLayout。這導致了異常。

最後我對兩個控制器使用了不同的類,這解決了在委託方法中選擇正確CollectionView的問題。

非常感謝您的輸入!

+3

我不能感謝你足夠!這真是讓我絕對是CrAzY!總而言之,當有多個UICollectionView存在時,會出現此錯誤,並且您對它們都使用相同的UICollectionViewFlowLayout實例。當我考慮它時是有道理的。每個UICollectionView都必須有它自己的UICollectionViewFlowLayout實例。謝謝!!! – NovaJoe

+0

這非常有幫助! –

0

您可以使用集合視圖的標籤屬性來確定哪個集合視圖是哪個集合視圖。像這樣:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { 

    NSLog(@"restorationIdentifier: %@", collectionView.restorationIdentifier); 

    if (collectionView.tag == collectionViewOneTag) 
    { 
     //create cell of type 1 
    } 
    else if (collectionView.tag == collectionViewTwoTag) 
    { 
     //create cell of type 2 
    } 

    return cell; 
} 

其中collectionViewOneTag和collectionViewTwoTag是可以在代碼或xib文件中定義的整數。

希望有所幫助。

+0

謝謝,但restoreIdentifier和Tag都不能在前兩種方法中識別 – Mathijs