一包券、在Storyboard中要進(jìn)行的操作
到Storyboard中馒吴,選擇collection view controller中的“Collection View”敞曹。
在storyBoard中選中collectionView
在Attributes inspector中沼侣,選擇”Section Header”和”Section Footer”,一旦選中你就會(huì)在看到collection 展示出了他的Header和他的Footer.
在Attributes inspector選中Section Header和Section Footer
在header和footer之間默認(rèn)為空丧靡,我們會(huì)用storyboard來設(shè)計(jì)視圖蟆沫。頭部是專門用來顯示一個(gè)部分的標(biāo)題,而底部視圖只顯示靜態(tài)橫幅圖片温治。
二饭庞、實(shí)現(xiàn)viewForSupplementaryElementOfKind方法
如果你嘗試運(yùn)行應(yīng)用程序,你可能不會(huì)看到header和footer罐盔,這是因?yàn)槲覀冞€沒有實(shí)現(xiàn)”viewFOrSupplementaryElementOfKind:”方法但绕。
代碼如下:
– ( UICollectionReusableView *)collectionView:( UICollectionView *)collectionView viewForSupplementaryElementOfKind:( NSString *)kind atIndexPath:( NSIndexPath *)indexPath
{
UICollectionReusableView *reusableview = nil ;
if (kind == UICollectionElementKindSectionHeader ){
CollectionHeaderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind : UICollectionElementKindSectionHeader withReuseIdentifier : @”HeaderView” forIndexPath :indexPath];
reusableview = headerView;
}
if (kind == UICollectionElementKindSectionFooter){
CollectionFooterView *footerview = [collectionView dequeueResuableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@”FooterView” forIndexPath:indexPath];
reusableview = footerview;
}
return reusableview;
}
上面的代碼告訴它頁眉/頁腳視圖應(yīng)該在每個(gè)部分中使用collect view。我們首先確定該集合視圖要求header或footer view。這可以通過使用一種變量來完成捏顺。對(duì)于頭來看六孵,我們出列header view(使用dequeueReusableSupplementaryViewOfKind :方法),并設(shè)置適當(dāng)?shù)臉?biāo)題和圖像幅骄。正如你可以從兩個(gè)if之間的代碼劫窒,我們使用我們之前分配給獲得header/footer view標(biāo)識(shí)符。