iOS-UICollectionView-頭視圖

  • 讓一個小伙伴說了排版的問題,重新進行了排版
純代碼添加UICollectionView的頭視圖,最近項目中用到這個雖然簡單記錄下供大家看看。
  • UICollectionView和tableview都需要遵從代理和數(shù)據(jù)源的方法。
  • tableview添加頭視圖
 self.tableView.tableHeaderView = [[UIView alloc]init];
相關(guān)代理方法
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{}
  • UICollectionView 創(chuàng)建之前需要先寫布局,每個collection都需要遵從這個布
    局。
UICollectionViewFlowLayout *layout =[[UICollectionViewFlowLayoutalloc]init];
layout.sectionInset =UIEdgeInsetsMake(0,0, 0, 0);
layout.headerReferenceSize =CGSizeMake(YJTViewWidth,50*YJTRatioH);//頭視圖大小
  • 然后注冊頭視圖
[_collection registerClass:[UICollectionReusableViewclass] forSupplementaryViewOfKind:UICollectionElementKindSectionHeaderwithReuseIdentifier:@"header"];
  • 最后是頭視圖相關(guān)的代理方法在里面處理邏輯布局問題
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
UICollectionReusableView *header = [collectionViewdequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeaderwithReuseIdentifier:@"header"forIndexPath:indexPath];
header.backgroundColor = RGB(236, 237,241);
if (indexPath.section ==0) {
labelOne.text =@"熱門檢查";
labelOne.font = [UIFontsystemFontOfSize:14.0f];
labelOne.textColor =MainRGB;
[header addSubview:labelOne];
}else{
labelTwo.text =@"疾病信息";
labelTwo.font = [UIFontsystemFontOfSize:14.0f];
labelTwo.textColor =MainRGB;
[header addSubview:labelTwo];
  }
return header;
   }
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
return CGSizeMake(頭視圖的寬, 頭視圖的高);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
return CGSizeMake(尾視圖的寬, 尾視圖的高);
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 翻譯自“Collection View Programming Guide for iOS” 0 關(guān)于iOS集合視...
    lakerszhy閱讀 4,083評論 1 22
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,692評論 4 61
  • 競賽時間和地點:2014年6月29日(星期日),上午9:00在貴陽奧體中心舉行。 如何到達主會場:乘坐28路、20...
    張揚的大翅膀閱讀 274評論 0 2
  • 心想日記1000字到底難不難呢?我自己覺得吧,這個問題對于我來說是有些難度的。因為要把字句組合連貫起來,要讀的通順...
    選妃閱讀 703評論 8 2
  • 今天本來是個有陽光的好日子,我卻因為看見一個我最不愿意看見的人而情緒低沉。因為她,那一份我刻意忘記卻刀刻于心的記憶...
    夢藤閱讀 246評論 0 1

友情鏈接更多精彩內(nèi)容