【问题标题】:Why can't I get a header for a UICollectionView?为什么我无法获得 UICollectionView 的标题?
【发布时间】:2015-01-28 21:05:22
【问题描述】:

我正在尝试创建分组图像的 UICollectionView(当前使用来自互联网的随机图像),每个组都有自己的标题。

我的问题是 - 我无法正确显示标题视图。
我将标题视图的背景颜色设置为黄色。

这是标题视图:

以下是包含两个组及其各自标题的当前集合:

如您所见,标题已调整为其成员单元格的大小并定位在集合中,而不是位于其预期的标题位置。

这是布局代码:

override func viewDidLoad() {

createFriends(sender:self)
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .Vertical;
layout.itemSize = CGSizeMake(50.0, 50.0);
layout.sectionInset = UIEdgeInsetsMake(10.0, 10.0, 50.0, 10.0);
layout.headerReferenceSize = CGSizeMake(320.0, 40.0)

layout.minimumLineSpacing = 10.0;
layout.minimumInteritemSpacing = 10.0;

gCollectionView.collectionViewLayout = layout

let myNib = UINib(nibName: "(1.1)HeaderViewCell",bundle: nil)
gCollectionView.registerNib(myNib, forCellWithReuseIdentifier: kCellIdentifier)
    }

这是标题代码(可能不需要参考尺寸,因为它是在布局中设置的):

func collectionView(collectionView: UICollectionView, layout 
          collectionViewLayout: UICollectionViewLayout,
          referenceSizeForHeaderInSection section: Int) -> CGSize {
     return CGSizeMake(100.0, 30.0)
}


func collectionView(collectionView: UICollectionView, 
                     viewForSupplementaryElementOfKind kind: String,
                     atIndexPath indexPath: NSIndexPath) -> 
                     UICollectionReusableView {


    let headerView = 
    collectionView.dequeueReusableCellWithReuseIdentifier(kCellIdentifier, 
         forIndexPath: indexPath) as mediaHeaderView

    headerView.backgroundColor = UIColor.yellowColor()  // ... YELLOW background
    return headerView          
}

问题:为什么我没有为每组项目获得正确定位的标题?

【问题讨论】:

  • 那些图片虽然是O_o

标签: swift uicollectionview


【解决方案1】:

Max K. 说得对;但做了几个错别字。

let myNib = UINib(nibName: "(1.1)HeaderCell",bundle: nil)
        gCollectionView.registerNib(myNib, forSupplementaryViewOfKind:UICollectionElementKindSectionHeader, withReuseIdentifier: kHeaderCellIdentifier)

func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {

        if kind == UICollectionElementKindSectionHeader {
            let headerView = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionHeader,
                withReuseIdentifier:kHeaderCellIdentifier, forIndexPath: indexPath) as mediaHeaderView
            return headerView
        } else {
            return UICollectionReusableView()
        }
}

【讨论】:

    【解决方案2】:

    您必须使用func registerNib(_ nib: UINib?, forSupplementaryViewOfKind kind: String, withReuseIdentifier identifier: String) 为标头注册nib

    试试这个:gCollectionView.registerNib(myNib, forSupplementaryViewOfKind:UICollectionElementKindSectionHeader, withReuseIdentifier: kHeaderReuseId)

    当然你必须使用func dequeueReusableSupplementaryViewOfKind(_ elementKind: String, withReuseIdentifier identifier: String, forIndexPath indexPath: NSIndexPath!) -> AnyObject 来检索可重用的标题视图

       func collectionView(collectionView: UICollectionView,
        viewForSupplementaryElementOfKind kind: String,
        atIndexPath indexPath: NSIndexPath) ->
        UICollectionReusableView {
    
            if kind == UICollectionElementKindSectionHeader {
                let headerView = collectionView.dequeReusableSupplementaryViewOfKind(UICollectionElementKindSectionHeader, withReuseIdentifier:kHeaderReuseId, forIndexPath: indexPath) as mediaHeaderView
    
                headerView.backgroundColor = UIColor.yellowColor()  // ... YELLOW background
                return headerView
            } else {
                assert(0) // shouldn't happen
                return UICollectionReusableView
            }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-11
      • 1970-01-01
      • 1970-01-01
      • 2018-06-04
      • 1970-01-01
      • 2013-02-09
      • 1970-01-01
      • 2020-09-01
      相关资源
      最近更新 更多