cellForItemAtIndexPath:在iOS7中激发,但在iOS6中没有



最近添加带有自定义UICollectionViewFlowLayout子类的UICollectionView时,collectionView:cellForItemAtIndexPath:方法仅在iOS7上调用,而在iOS6上不调用。换句话说,在iOS7中一切都很好,但我的自定义收藏视图项目没有显示在iOS6中。有趣的是,单元格似乎在那里(collectionView滚动),但所有项目都是空的,背景是白色。

collectionView是在.xib文件中设置的,dataSource和delegate已附加,UICollectionViewDataSource和UICollectionViewDelegateFlowLayout是在视图控制器.h文件中的@interface调用之后添加的。

collectionView项大小、节插入、行间距和项间间距都是在自定义流布局init方法中设置的。

部分代码:

- (void)viewDidLoad
{
[super viewDidLoad];
self.collectionView.collectionViewLayout = [[TFSpringFlowLayout alloc] init];
[self.collectionView registerClass:[TFWorkoutCell class] forCellWithReuseIdentifier:CellIdentifier];
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
// This method is returning a value > 0
return _workouts.count;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{
// This is being called on iOS7, but is never being called on iOS6
...removed for clarity
return cell;
}

编辑:问题已解决。我的自定义Flow Layout包含了一些iOS7特定的覆盖,使用了新的UIDynamicAnimator类。这些并没有导致崩溃,但阻止了细胞在iOS6中被提取。

以下是我遇到的问题,以防将来有其他人遇到这个问题。

我的自定义UICollectionViewFlowLayout包含几个方法重写,以实现iOS7的新UIKit Dynamics。这些并没有导致应用程序崩溃,但阻止了在iOS6中提取细胞。

以下是违规代码:

-(NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
return [self.dynamicAnimator itemsInRect:rect];
}
-(UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
return [self.dynamicAnimator layoutAttributesForCellAtIndexPath:indexPath];
}

修复所需的简单更改:

-(NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
if ([UIDynamicAnimator class])
return [self.dynamicAnimator itemsInRect:rect];
else
return [super layoutAttributesForElementsInRect:rect];
}
-(UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
if ([UIDynamicAnimator class])
return [self.dynamicAnimator layoutAttributesForCellAtIndexPath:indexPath];
else
return [super layoutAttributesForItemAtIndexPath:indexPath];
}

添加if/else语句来检查iOS6或iOS7,并且只返回适当的响应,这为我解决了问题。希望这能帮助其他人!

最新更新