UICollectionView(網(wǎng)格布局)

初始化

@property (nonatomic, strong) UICollectionView *collectionView;
- (UICollectionView *)collectionView
{
    if (!_collectionView)
    {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.sectionInset = UIEdgeInsetsMake(0.0f, 0.0f, 0.0f, 0.0f);//每個區(qū)的空隙
        layout.minimumInteritemSpacing = 10; //列與列之間的間距
        layout.minimumLineSpacing = 10;//行與行之間的間距
        layout.itemSize = CGSizeMake((SCREEN_WIDTH - 10)/2, 200);//cell的大小
        
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.delegate = self.manager;
        _collectionView.dataSource = self.manager;
        [_collectionView registerClass:[YZGMJCollectionCell class] forCellWithReuseIdentifier:@"YZGMJCollectionCell"];
    }
    return _collectionView;
}

[self.view addSubview:self.collectionView];
[_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
 }];

<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) NSMutableArray *dataArray;

- (NSMutableArray *)dataArray{
    if(!_dataArray){
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

常用代理方法

#pragma mark ************** CollectionView 代理方法
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
   
    YZGMJCollectionCell *cell = (YZGMJCollectionCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"YZGMJCollectionCell" forIndexPath:indexPath];
    cell.cellClickBlack = ^(NSString *title){
        self.cellClickBlack(title);
    };
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSLog(@"%ld被點擊",indexPath.row);
    
}

注冊cell,SectionHead,SectionFoot

 [_collectionView registerClass:[YZGMJCollectionCell class] forCellWithReuseIdentifier:@"YZGMJCollectionCell"];
 [_collectionView registerClass:[YZGMJSectionHeadView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"YZGMJSectionHeadView"];
  [_collectionView registerClass:[YZGMJSectionFootView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"YZGMJSectionFootView"];

UINib * sectionHeadNib = [UINib nibWithNibName:@"SmartBoxClvSectionHeadView" bundle:nil];
[collectionView registerNib: sectionHeadNib forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"SmartBoxClvSectionHeadView"];

自定義 分區(qū)頭部,尾部代理

#pragma mark ************** sectionHeadFootView
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    ESWeakSelf;
    UICollectionReusableView *reusableview = nil;
    if (kind == UICollectionElementKindSectionHeader )//頭部
    {
        
        YZGMJSectionHeadView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"YZGMJSectionHeadView" forIndexPath:indexPath];

         reusableview = headerView;
        
    }
    if(kind == UICollectionElementKindSectionFooter)//尾部
    {
        YZGMJSectionFootView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"YZGMJSectionFootView" forIndexPath:indexPath];
        
        reusableview = footerView;
    
    }
    
    return reusableview;
    
    
}

#pragma mark ************** sectionHeadView 尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
      return CGSizeMake(SCREEN_WIDTH,50);
}

#pragma mark ************** sectionFootView 尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
{
      return CGSizeMake(SCREEN_WIDTH,50);
}

item Size 代理

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(SCREEN_WIDTH/2,100);
}

collectionView 方法特性

##水平滑動
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
##滾動到頂部
 [_collectionViewList scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
UICollectionViewScrollPositionNone  有內(nèi)邊距可修改為 bottom 才能正確移動到指定位置

##動態(tài)計算CollectionView 的行數(shù)
NSInteger row = titles.count % 3 ? titles.count / 3 + 1 : titles.count / 3;

#內(nèi)容少也可滑動
_clvData.alwaysBounceVertical = YES;
#當(dāng)水平滾條到達(dá)終點所宰,總是(視圖)彈跳
_collectionView.alwaysBounceHorizontal = YES;


遇到一些問題

UICollectionReusableView  用IB 注冊的時候要拖線綁定才能正確顯示出來
itemSize 的 width 取整
布局更新時候調(diào)用此方法绒尊,cell 插入 CollectionView 
self.collectionView.collectionViewLayout.invalidateLayout() 

---------獲取clv 內(nèi)容高度
CGFloat height = self.clvData.collectionViewLayout.collectionViewContentSize.height;
---------tbvCell 嵌套 clv
CustomBasicInfoVC

iOS UICollectionView等分有1px縫隙

-(CGSize)collectionView:(UICollectionView* )collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger num = 4;
    CGFloat width = CGRectGetWidth(collectionView.bounds)/num;
    CGFloat height = 98;
    if(indexPath.row == 0){
        //第一列的width比其他列稍大一些,消除item之間的間隙
        CGFloat realWidth = CGRectGetWidth(collectionView.bounds) - floor(width) * (num - 1);
        return CGSizeMake(realWidth, height);
    }else{
        return CGSizeMake(floor(width), height);
    }
}

UICollectionView解決item之間的間隙問題
https://blog.csdn.net/ayuapp/article/details/80360745

UICollectionView橫向滾動刷新控件
https://blog.csdn.net/weixin_42657552/article/details/81035005
iOS 為CollectionView的分區(qū)添加背景色
https://blog.csdn.net/github_28024665/article/details/64125233

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末仔粥,一起剝皮案震驚了整個濱河市垒酬,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌件炉,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,036評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件矮湘,死亡現(xiàn)場離奇詭異斟冕,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)缅阳,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,046評論 3 395
  • 文/潘曉璐 我一進(jìn)店門磕蛇,熙熙樓的掌柜王于貴愁眉苦臉地迎上來景描,“玉大人,你說我怎么就攤上這事秀撇〕祝” “怎么了?”我有些...
    開封第一講書人閱讀 164,411評論 0 354
  • 文/不壞的土叔 我叫張陵呵燕,是天一觀的道長棠绘。 經(jīng)常有香客問我,道長再扭,這世上最難降的妖魔是什么氧苍? 我笑而不...
    開封第一講書人閱讀 58,622評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮泛范,結(jié)果婚禮上让虐,老公的妹妹穿的比我還像新娘。我一直安慰自己罢荡,他們只是感情好赡突,可當(dāng)我...
    茶點故事閱讀 67,661評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著区赵,像睡著了一般惭缰。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上惧笛,一...
    開封第一講書人閱讀 51,521評論 1 304
  • 那天从媚,我揣著相機(jī)與錄音,去河邊找鬼患整。 笑死拜效,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的各谚。 我是一名探鬼主播紧憾,決...
    沈念sama閱讀 40,288評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼昌渤!你這毒婦竟也來了赴穗?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,200評論 0 276
  • 序言:老撾萬榮一對情侶失蹤膀息,失蹤者是張志新(化名)和其女友劉穎般眉,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體潜支,經(jīng)...
    沈念sama閱讀 45,644評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡甸赃,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,837評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了冗酿。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片埠对。...
    茶點故事閱讀 39,953評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡络断,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出项玛,到底是詐尸還是另有隱情貌笨,我是刑警寧澤,帶...
    沈念sama閱讀 35,673評論 5 346
  • 正文 年R本政府宣布襟沮,位于F島的核電站锥惋,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏臣嚣。R本人自食惡果不足惜净刮,卻給世界環(huán)境...
    茶點故事閱讀 41,281評論 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望硅则。 院中可真熱鬧淹父,春花似錦、人聲如沸怎虫。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,889評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽大审。三九已至蘸际,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間徒扶,已是汗流浹背粮彤。 一陣腳步聲響...
    開封第一講書人閱讀 33,011評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留姜骡,地道東北人导坟。 一個月前我還...
    沈念sama閱讀 48,119評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像圈澈,于是被迫代替她去往敵國和親惫周。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,901評論 2 355

推薦閱讀更多精彩內(nèi)容