自定义UICollectionView
1.創(chuàng)建一個(gè)UICollectionView工程,點(diǎn)擊鼠標(biāo)右側(cè)按鈕選擇New File->Cocoa Class->點(diǎn)擊Next,Class選項(xiàng)填寫一個(gè)合理的名稱,如:MyCollectionViewCell,然后點(diǎn)擊Next。
?
2.AppDelegate.m文件中導(dǎo)入頭文件“#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];
? ? //設(shè)置headerView的尺寸大小
? ? layout.headerReferenceSize = CGSizeMake(WIDTH, 0);
? ? //該方法也可以設(shè)置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;
}
?
//設(shè)置每個(gè)item的尺寸
//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
//? ? return CGSizeMake(90, 130);
//}
?
//設(shè)置每個(gè)item的UIEdgeInsets
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
? ? return UIEdgeInsetsMake(10, 10, 10, 10);
}
?
//如果一組中有多行item,設(shè)置行間距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
? ? return 10;
}
?
//設(shè)置兩個(gè)組之間的列間距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
? ? return 15;
}
?
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
? ? //width的設(shè)置對該方法無影響
? ? return CGSizeMake(300, 30);
}
?
//通過設(shè)置SupplementaryViewOfKind 來設(shè)置頭部或者底部的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];
? ? //解決重用機(jī)制的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.效果圖如下:
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/Yun-Longcom/p/5607988.html
總結(jié)
以上是生活随笔為你收集整理的自定义UICollectionView的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: elf文件中的.plt .rel.dyn
- 下一篇: $.ajax() 方法的理解