我在一个 UIViewController 中有两个 UICollectionView。我用标签号分隔它们,以便我可以同时使用数据源和委托(delegate)方法。但是,当我运行代码时,它会因异常而崩溃:
由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“UICollectionView 接收到具有不存在索引路径的单元格的布局属性: {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)numberOfSectionsInCollectionViewUICollectionView *)collectionView
{
return 1;
}
- (NSInteger)collectionViewUICollectionView *)collectionView numberOfItemsInSectionNSInteger)section
{
if (collectionView.tag == 200)
{
return self.children.count;
} else if (collectionView.tag == 201){
return self.socialMediaGrayIcons.count;
}
return 1;
}
- (UICollectionViewCell *)collectionViewUICollectionView *)collectionView cellForItemAtIndexPathNSIndexPath *)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;
}
Best Answer-推荐答案 strong>
采纳@Paulw 的好建议如下所示:
@property(weak,nonatomic) IBOutlet UICollectionView *collectionViewA;
@property(weak,nonatomic) IBOutlet UICollectionView *collectionViewB;
您的数据源方法必须严格按照传递的 Collection View 将条件划分为两个分支,并且始终在一个中使用一个数据源数组,在另一个中使用另一个。
您可以通过始终通过方便的方法获取数据源来强制执行此宗教,例如...
- (NSArray *)datasourceForCollectionViewUICollectionView *)collectionView {
if (collectionView == self.collectionViewA) {
return self.children;
} else { // NOTICE - no else-if, there's no other valid condition
return self.socialMediaGrayIcons;
}
}
在任何地方都可以使用它,例如...
- (NSInteger)collectionViewUICollectionView *)collectionView numberOfItemsInSectionNSInteger)section {
return [self datasourceForCollectionView:collectionView].count;
}
关于IOS UICollectionView 在使用两个 Collection View 时抛出断言,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/43195498/
|