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事件