为什么我无法获得UICollectionView的标头?

时间:2015-01-28 21:05:23

标签: swift uicollectionview

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

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

这是标题视图:
enter image description here  
以下是包含两个组及其各自标题的当前集合:

enter image description here

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

这是布局代码:

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          
}

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

2 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分: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
        }
}