6

我正在尝试向 UICollectionView 添加页脚。

以下是我的代码,

UICollectionView是通过IB添加的

在 viewDidLoad 我注册页脚,

[mCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer"];

并实现了以下方法

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView   viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *reusableview = nil;

    if (kind == UICollectionElementKindSectionFooter) {
      UICollectionReusableView *headerView = [mCollectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"footer" forIndexPath:indexPath];

      [headerView addSubview:mFooterView];
      reusableview = headerView;
   }
   return reusableview;
}

但是我的应用程序不断崩溃,下面是日志,

*** -[UICollectionView _dequeueReusableViewOfKind:withIdentifier:forIndexPath:] 中的断言失败,/SourceCache/UIKit/UIKit-2380.17/UICollectionView.m:2249

任何帮助表示赞赏。

谢谢。

4

2 回答 2

13

在您的代码中,为什么要对标头视图进行出队并将页脚添加到其中?

该方法的正常实现是:

- (UICollectionReusableView *)collectionView:(UICollectionView *)theCollectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)theIndexPath 
{

   UICollectionReusableView *theView;

   if(kind == UICollectionElementKindSectionHeader)
   {
      theView = [theCollectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:theIndexPath];
   } else {
      theView = [theCollectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer" forIndexPath:theIndexPath];
   }

   return theView;
}
于 2013-10-23T10:28:26.680 回答
0

对于斯威夫特 4

 override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        var myView = UICollectionReusableView()
        if kind == UICollectionView.elementKindSectionHeader {
            myView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: myHeader, for: indexPath)
        } else {
            myView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: myFooter, for: indexPath)
        }
            return myView
        }
于 2019-05-16T12:45:09.713 回答