Mat*_*ijs 3 xcode objective-c ios
我有一个问题,我现在几个小时都无法解决......
我有多个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];
Run Code Online (Sandbox Code Playgroud)
我的功能是这样的:
- (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;
}
Run Code Online (Sandbox Code Playgroud)
在我的日志中,我得到以下输出(例如):
numberOfItemsInSection中的restorationIdentifier:(null)在sizeForItemAtIndexPath中的restorationIdentifier :( null)restorationIdentifier cellForItemAtIndexPath:collectionViewOne
collectionViewOne是collectionViewOne上的restorationIdentifier.那么为什么前两种方法没有认识到呢?
当然,结果是我崩溃了,因为不正确设置了不同CollectionViews中的单元格和单元格数量.错误:
*** 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}'
Run Code Online (Sandbox Code Playgroud)
我怎样才能解决这个问题?
Mat*_*ijs 10
这解决了.问题在于我对两个UICollectionView使用了相同的UICollectionViewFlowLayout.这引起了例外.
最后,我为两个控制器使用了不同的类,这解决了在委托方法中选择正确的CollectionView的问题.
谢谢大家的投入!
| 归档时间: |
|
| 查看次数: |
3711 次 |
| 最近记录: |