zoukankan      html  css  js  c++  java
  • iOS 常用代码之 UICollectionView

    记一下 不用每次都从0开始写,人生苦短 ,省点时间给自己



    之前必须完成相关注册:
    1. cell 2. 头部和尾部 [self.hotAndHistoryCollectionV registerNib:[UINib nibWithNibName:@"HotAndHistoryCell" bundle:nil] forCellWithReuseIdentifier:@"hotSearchCellID"]; [self.hotAndHistoryCollectionV registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headViewID"]; #pragma mark --- hot search collectionViewDelegate --- - (nonnull __kindof UICollectionViewCell *)collectionView:(nonnull UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath { HotAndHistoryCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"hotSearchCellID" forIndexPath:indexPath]; //cell.backgroundColor = [UIColor redColor]; //cell.contentView.backgroundColor = [UIColor blackColor]; return cell; } - (NSInteger)collectionView:(nonnull UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return 6; } - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 2; } -(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{ if ([kind isEqual:UICollectionElementKindSectionHeader]) { //UIView *headView = [[UIView alloc]init]; UICollectionReusableView *headView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"headViewID" forIndexPath:indexPath]; if (indexPath.section == 0) { UILabel *label = [[UILabel alloc] init]; label.frame = CGRectMake(12,10,110,20); label.text = @"热门搜索"; label.font = [UIFont fontWithName:@"PingFang-SC-Regular" size:14]; label.textColor = [UIColor colorWithRed:21/255.0 green:23/255.0 blue:27/255.0 alpha:1]; [headView addSubview:label]; } else { UIView *view = [[UIView alloc] init]; view.frame = CGRectMake(0,0,self.view.frame.size.width,8); view.backgroundColor = [UIColor colorWithRed:249/255.0 green:249/255.0 blue:251/255.0 alpha:1]; [headView addSubview:view]; UILabel *label = [[UILabel alloc] init]; label.frame = CGRectMake(12,18,110,20); label.text = @"历史搜索"; label.font = [UIFont fontWithName:@"PingFang-SC-Regular" size:14]; label.textColor = [UIColor colorWithRed:21/255.0 green:23/255.0 blue:27/255.0 alpha:1]; [headView addSubview:label]; UIButton *trashBtn = [UIButton new]; trashBtn.frame = CGRectMake(headView.frame.size.width - 12 - 20, 18, 20, 20); [trashBtn setImage:[UIImage imageNamed:@"search_iconbtn_delete_default"] forState:UIControlStateNormal]; [headView addSubview:trashBtn]; } return headView; } return nil; } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{ if (section == 0) { return CGSizeMake(self.view.frame.size.width, 30); } else{ return CGSizeMake(self.view.frame.size.width, 38); } } //定义每个UICollectionView 的大小 - ( CGSize )collectionView:( UICollectionView *)collectionView layout:( UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:( NSIndexPath *)indexPath { return CGSizeMake((self.view.frame.size.width - 12 * 6)/3,34); } //定义每个UICollectionView 的边距 - ( UIEdgeInsets )collectionView:( UICollectionView *)collectionView layout:( UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:( NSInteger )section { return UIEdgeInsetsMake ( 9 , 12 , 9 , 12 ); } ////设置水平间距 (同一行的cell的左右间距) //-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section { // return 9; //} ////垂直间距 (同一列cell上下间距) //- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section { // return 9; //}
    此文仅为鄙人学习笔记之用,朋友你来了,如有不明白或者建议又或者想给我指点一二,请私信我。liuw_flexi@163.com/QQ群:582039935. 我的gitHub: (学习代码都在gitHub) https://github.com/nwgdegitHub/
  • 相关阅读:
    慈不掌兵,义不行贾,烂好人难成大业!
    克服焦虑
    静态路由汇总(路由聚合)
    OSPF协议介绍及配置 (下)
    OSPF协议介绍及配置 (上)
    我为什么鼓励工程师写blog
    佛弟子有三样东西需要永远保密
    【交换机在江湖】第十三章 VLAN划分篇
    FileZilla Server ftp 服务器下通过alias别名设置虚拟目录(多个分区)
    Java 对象和类
  • 原文地址:https://www.cnblogs.com/liuw-flexi/p/9151569.html
Copyright © 2011-2022 走看看