【问题标题】:UICollectionView FooterUICollectionView 页脚
【发布时间】:2013-07-26 13:21:25
【问题描述】:

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

以下是我的代码,

UICollectionView是通过IB添加的

IN 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

感谢任何帮助。

谢谢。

【问题讨论】:

    标签: ios objective-c uicollectionview


    【解决方案1】:

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

    这个方法的正常实现是:

    - (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;
    }
    

    【讨论】:

    • 是的,我做了同样的事情,它对我有用!!我会将你的答案标记为正确
    【解决方案2】:

    对于 Swift 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
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多