• 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/
  • 相关阅读:
    WeChat-SmallProgram:组件 scroll-view 横向和纵向 案例
    Codeforces Round #277 (Div. 2) D. Valid Sets 树形DP
    Codeforces Round #243 (Div. 2) E. Sereja and Two Sequences DP
    Codeforces Round #263 (Div. 2) D. Appleman and Tree 树形dp
    Codeforces Round #259 (Div. 2) D. Little Pony and Harmony Chest 状压DP
    Codeforces Round #274 (Div. 2) E. Riding in a Lift DP
    HDOJ 6069 素数筛
    在平面中,一个点绕任意点旋转θ度后的点的坐标
    HDOJ 5724 博弈SG函数
    Codeforces Round #422 (Div. 2) C. Hacker, pack your bags! 排序+贪心
  • 原文地址:https://www.cnblogs.com/liuw-flexi/p/9151569.html
Copyright © 2020-2023  润新知