日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

自定义UICollectionView

發布時間:2025/4/14 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 自定义UICollectionView 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.創建一個UICollectionView工程,點擊鼠標右側按鈕選擇New File->Cocoa Class->點擊Next,Class選項填寫一個合理的名稱,如:MyCollectionViewCell,然后點擊Next。

?

2.AppDelegate.m文件中導入頭文件“#import “ViewController.h””,然后填寫如下代碼:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

? ? UINavigationController *nav=[[UINavigationController alloc]initWithRootViewController:[[ViewController alloc]init]];

? ? self.window.rootViewController=nav;

? ? return YES;

}

?

?3.ViewController.m文件代碼

#import "ViewController.h"

#import "MyCollectionViewCell.h"

#import "Header.h"

?

@interface ViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>{

? ? UICollectionView? *mainCollectionView;

}

?

@end

?

@implementation ViewController

?

- (void)viewDidLoad {

? ? [super viewDidLoad];

? ? self.view.backgroundColor=[UIColor whiteColor];

? ? self.navigationController.navigationBar.translucent=NO;

? ? self.navigationController.navigationBar.barTintColor=[UIColor purpleColor];

?? ?

? ? UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc]init];

? ? //設置headerView的尺寸大小

? ? layout.headerReferenceSize = CGSizeMake(WIDTH, 0);

? ? //該方法也可以設置itemSize

? ? layout.itemSize =CGSizeMake(90, 150);

?? ?

? ? mainCollectionView=[[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:layout];//初始化

? ? //注冊UICollectionViewCell

? ? [mainCollectionView registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];

? ? [mainCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"reusableView"];

? ? mainCollectionView.dataSource=self;

? ? mainCollectionView.delegate=self;

? ? mainCollectionView.backgroundColor=[UIColor whiteColor];

? ? [self.view addSubview:mainCollectionView];

}

?

?

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{

? ? return 3;

}

?

?

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

? ? return 9;

}

?

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

? ? static NSString *identifier=@"cell";

? ? MyCollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];

? ? cell.nameLable.text=[NSString stringWithFormat:@"{%ld,%ld}",(long)indexPath.section,(long)indexPath.row];

? ? cell.imageView.image=[UIImage imageNamed:@"photo"];

? ? cell.backgroundColor=[UIColor yellowColor];

? ? return cell;

}

?

//設置每個item的尺寸

//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{

//? ? return CGSizeMake(90, 130);

//}

?

//設置每個item的UIEdgeInsets

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{

? ? return UIEdgeInsetsMake(10, 10, 10, 10);

}

?

//如果一組中有多行item,設置行間距

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{

? ? return 10;

}

?

//設置兩個組之間的列間距

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{

? ? return 15;

}

?

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{

? ? //width的設置對該方法無影響

? ? return CGSizeMake(300, 30);

}

?

//通過設置SupplementaryViewOfKind 來設置頭部或者底部的view,其中 ReuseIdentifier 的值必須和 注冊是填寫的一致,本例都為 “reusableView”

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath

{

? ? UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"reusableView" forIndexPath:indexPath];

? ? headerView.backgroundColor =[UIColor grayColor];

? ? //解決重用機制的bug

? ? for (UIView *view in headerView.subviews) {

? ? ? ? [view removeFromSuperview];

? ? }

? ? UILabel *label = [[UILabel alloc] initWithFrame:headerView.bounds];

? ? if (indexPath.section==0) {

? ? ? ? label.text = @"食品類";

? ? }

? ? if (indexPath.section==1) {

? ? ? ? label.text = @"水果類";

? ? }

? ? if (indexPath.section==2) {

? ? ? ? label.text = @"家用類";

? ? }

? ? label.font = [UIFont systemFontOfSize:20];

? ? [headerView addSubview:label];

? ? return headerView;

}

@end

?

4.MyCollectionView.h文件代碼

#import <UIKit/UIKit.h>

@interface MyCollectionViewCell : UICollectionViewCell

@property(nonatomic,strong)UIImageView *imageView;

@property(nonatomic,strong)UILabel ? ? *nameLable;

@end

?

5.MyCollectionView.m文件代碼

#import "MyCollectionViewCell.h"

?

@implementation MyCollectionViewCell

?

-(instancetype)initWithFrame:(CGRect)frame{

? ? self=[super initWithFrame:frame];

? ? if (self) {

? ? ? ? _imageView=[[UIImageView alloc]initWithFrame:CGRectMake(10, 0, 70, 70)];

? ? ? ? [self addSubview:_imageView];

?? ? ? ?

? ? ? ? _nameLable=[[UILabel alloc]initWithFrame:CGRectMake(10, 80, 70, 30)];

? ? ? ? _nameLable.textAlignment=NSTextAlignmentCenter;

? ? ? ? _nameLable.textColor=[UIColor blueColor];

? ? ? ? _nameLable.font=[UIFont systemFontOfSize:16];

? ? ? ? _nameLable.backgroundColor=[UIColor grayColor];

? ? ? ? [self addSubview:_nameLable];

?

? ? }

? ? return self;

}

@end

?

6.效果圖如下:

?

?

轉載于:https://www.cnblogs.com/Yun-Longcom/p/5607988.html

總結

以上是生活随笔為你收集整理的自定义UICollectionView的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。