使用UICollectionView遇到的各种坑



1)头视图和尾部视图的添加
UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView" forIndexPath:indexPath];

UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footerView" forIndexPath:indexPath];

2)内嵌(需求就是UICollectionView没有像Tableview一样的TabHeaderView),想要制造一个

contentInset


3)没有注册
[_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView"];
[_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footerView"];
2.使用UICollectionView遇到的复用问题

1)头视图的使用
for (UIView *view in headerView.subviews) {
[view removeFromSuperview];
}

for (UIView *view in footerView.subviews) {
[view removeFromSuperview];
}

2)cell的复用

KnowledgeBasePopCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"KnowledgeBasePopCell" forIndexPath:indexPath];


3.使用UICollectionView不走代理的问题

1)item尺寸计算错误



2)禁止使用0.01这种尺寸

//每个item之间的间距
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{

return 0.01;
}

//定义每个Section 的 margin
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{


return UIEdgeInsetsMake(0.01,0.01,0.01,0.01);
}


上述两个是不走-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{



3)在window上添加view,view上添加UICollectionView,是不走-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{


使用各种手势冲突判断解决方法,但是都没有效果

是UICollectionViewCell视图?
是UICollectionView类?
都不能捕捉到点击事件

解决方法:
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{

if ([touch.view isDescendantOfView:self.collectionView]) {
return NO;
}
return YES;
}

4。使用UICollectionView组头也可以悬停

UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
//header
flowLayout.sectionHeadersPinToVisibleBounds = YES;
//footer
flowLayout.sectionFootersPinToVisibleBounds = YES;

问题:这仅在iOS9中才支持这种设置

by:ml

猜你喜欢

转载自www.cnblogs.com/widgetbox/p/10045362.html