2017-04-03 65 views
2

我在單個UIViewController中有兩個UICollectionViews。我通過標記號將它們分開,以便我可以爲兩者使用數據源和委託方法。但是,當我運行代碼時,它會與異常崩潰:IOS UICollectionView在使用兩個集合視圖時拋出斷言

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UICollectionView received layout attributes for a cell with an index path that does not exist: <NSIndexPath: 0xc000000000200016> {length = 2, path = 0 - 1}'

我在論壇看了這個,大多數人說你需要無效然後重新加載UIControllerView,但在我的情況下,這是行不通的。

任何人都知道如何解決這個問題?

這裏是我的代碼:

-(void)viewDidLoad { 

    self.socialMediaGrayIcons = [[NSMutableArray alloc] initWithObjects:[UIImage imageNamed:@"fb-gray.png"], 
          [UIImage imageNamed:@"twitter-gray.png"], 
          [UIImage imageNamed:@"insta-gray.png"], 
          [UIImage imageNamed:@"sms-gray.png"], 
          [UIImage imageNamed:@"email-gray.png"], nil]; 

    // setup collection view 
    self.avatarCollectionView.tag = 200; 
    self.socialMediaCollectionView.tag = 201; 

    UINib *cellNib = [UINib nibWithNibName:@"NibCell" bundle:nil]; 
    [self.avatarCollectionView registerNib:cellNib forCellWithReuseIdentifier:@"cvCell"]; 
    [self.socialMediaCollectionView registerNib:cellNib forCellWithReuseIdentifier:@"smCell"]; 

    // setup collection view layout 
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init]; 
    [flowLayout setItemSize:CGSizeMake(40, 40)]; 
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal]; 

    [self.avatarCollectionView setCollectionViewLayout:flowLayout]; 
    [self.socialMediaCollectionView setCollectionViewLayout:flowLayout]; 

    [self.avatarCollectionView reloadData]; 
    [self.avatarCollectionView.collectionViewLayout invalidateLayout]; 

    [self.socialMediaCollectionView reloadData]; 
    [self.socialMediaCollectionView.collectionViewLayout invalidateLayout]; 
} 

.... 

#pragma mark UICollectionView DataSource and Delegate mathods 
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView 
{ 
    return 1; 
} 

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section 
{ 
    if (collectionView.tag == 200) 
    { 
     return self.children.count; 
    } else if (collectionView.tag == 201){ 
     return self.socialMediaGrayIcons.count; 
    } 

    return 1; 
} 


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

    if (collectionView.tag == 200) 
    { 
     static NSString *cellIdentifier = @"cvCell"; 
     cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath]; 

     Child *currentChild = [self.children objectAtIndex:indexPath.row]; 


     UIImage *curImage = [UIImage imageWithData:currentChild.thumbnail]; 
     UIImageView *thumbView = (UIImageView *)[cell viewWithTag:100]; 

     if (curImage != nil) 
     { 
      [thumbView setImage:curImage]; 

     } 
    } else if (collectionView.tag == 201){ 

     static NSString *cellIdentifier = @"smCell"; 
     cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath]; 

     UIImage *curImage = (UIImage*) [self.socialMediaGrayIcons objectAtIndex:indexPath.row]; 
     UIImageView *thumbView = (UIImageView *)[cell viewWithTag:101]; 

     if (curImage != nil) 
     { 
      [thumbView setImage:curImage]; 

     } 
    } 

    return cell; 
} 
+2

這裏有一些證據你的'numberOfItemsInSection'正在下降到底部的無意義的'return 1',並且其中一個數組有0個元素。 – danh

+4

請勿使用標籤。將每個集合視圖簡單地存儲爲屬性,然後將收到的collectionview參數與屬性進行比較,以確定您被問到哪個集合視圖 – Paulw11

+0

This [Answer](http://stackoverflow.com/questions/18371184/how-do -i-add-two-collectionviews-on-one-viewcontroller)覆蓋你的問題。希望它有幫助... –

回答

2

以@ Paulw的好建議是這樣的:

@property(weak,nonatomic) IBOutlet UICollectionView *collectionViewA; 
@property(weak,nonatomic) IBOutlet UICollectionView *collectionViewB; 

你的數據源的方法必須是宗教有關基於條件的兩個分支總是分它們傳遞的集合視圖,並且始終使用一個數據源數組和另一個數據源數組。

您可以隨時通過方便的方法讓您的數據源,這樣執行這個宗教...

- (NSArray *)datasourceForCollectionView:(UICollectionView *)collectionView { 
    if (collectionView == self.collectionViewA) { 
     return self.children; 
    } else { // NOTICE - no else-if, there's no other valid condition 
     return self.socialMediaGrayIcons; 
    } 
} 

使用它無處不在,在...

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { 
    return [self datasourceForCollectionView:collectionView].count; 
} 
+0

如何使用便利方法 - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath其中一個數組的持有UIImage對象和其他子對象? –

+0

在條件之前使用便捷方法獲取數組。然後在indexPath.row中引用數組,並在每個分支中適當地轉換該元素。 – danh

+0

進行建議更改後仍然會崩潰 –

相關問題