日韩性视频-久久久蜜桃-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的全部內容,希望文章能夠幫你解決所遇到的問題。

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

主站蜘蛛池模板: 小sao货cao死你 | 日韩精品一二三四 | 欧洲一区二区三区在线 | 欧美黑人又粗又大的性格特点 | 欧州一区二区 | 国产极品美女高潮无套嗷嗷叫酒店 | 欧美午夜精品久久久久免费视 | 久久精品23| 日韩综合一区 | 五月天婷婷色综合 | 国产青草| 亚洲老女人av | 国产婷婷综合 | 男人插女人的网站 | 好屌妞视频这里有精品 | 在线观看的免费 | 欧美日韩在线视频一区二区 | 欧美一级免费黄色片 | 美女视频黄a视频全免费观看 | www.色在线观看 | 久久精品视频在线播放 | 精品欧美一区二区久久久 | 国产乱码精品一区二区三区亚洲人 | 黄色在线免费视频 | 欧美成人精品欧美一 | 大胸美女无遮挡 | 狠狠操女人 | av中文字幕在线免费观看 | 国产一区二区欧美日韩 | 天堂av最新网址 | 男人女人拔萝卜视频 | 成人网免费视频 | 人妻少妇精品视频一区二区三区 | 久久久久久亚洲中文字幕无码 | 国产人妻精品久久久久野外 | 99精品欧美一区二区 | 日本中文字幕在线播放 | 欧美黄色大片视频 | 靠逼视频网站 | 国产吞精囗交免费视频网站 | 538任你躁在线精品免费 | 免费处女在线破视频 | 中文字幕人妻互换av久久 | 国产一区二区日韩 | 一区二区三区视频在线观看 | 日韩色视频在线观看 | 一级片在线免费 | 7777在线视频| fc2ppv色の美マンに中出し | 黄色网页网站 | 欧美成人一二三 | 日韩视频精品 | 日本三级少妇 | 久久香蕉av | 国产一区视频免费观看 | 美国黄色一级大片 | 日韩成人自拍 | 解开乳罩喂领导吃奶 | 无码成人精品区一级毛片 | 欧美日韩性视频 | 四虎首页| 开心激情五月婷婷 | 午夜精品偷拍 | 久青草视频在线观看 | 精品久久国产字幕高潮 | 五月婷婷天堂 | 国产三区在线观看 | 在线免费观看视频a | 一进一出好爽视频 | 成人区视频 | 丰满人妻一区二区三区免费 | 性天堂网| 久久1234| 欧美91av| 午夜99| 五月天男人天堂 | 韩国禁欲系高级感电影 | 亚洲成人一二区 | 久久avav| www.天堂av.com| 色窝网| 一区精品在线 | 欧美丝袜脚交 | 国产精品一区二区三 | 韩国av免费在线观看 | 久久成人高清 | 欧美一级生活片 | 欧美日韩成人一区二区 | 男人插入女人下面的视频 | 欧美成人短视频 | 毛片一区二区 | 69色视频 | 日产av在线 | 天堂av观看 | 久久婷婷网 | 奇米成人网 | 天天干天天操天天射 | 麻豆国产一区 | 波多野吉衣av |