网格

2017-10-25  本文已影响0人  法库德

#import "ViewController.h"

#import "myCollectionViewCell.h"

<UICollectionViewDelegate,UICollectionViewDataSource>

{

UIPageControl *thePage;

UICollectionView *theCollect;

}

[super viewDidLoad];

//设置流布局对象;

UICollectionViewFlowLayout *theFlow=[[UICollectionViewFlowLayout alloc]init];

//设置网格的大小;

theFlow.itemSize=CGSizeMake(80, 90);

//设置最小行间距和最小列间距;

theFlow.minimumInteritemSpacing=20 ;

theFlow.minimumLineSpacing=20;

//设置滚动的方向、

theFlow.scrollDirection=UICollectionViewScrollDirectionHorizontal;

//设置页边距;

theFlow.sectionInset=UIEdgeInsetsMake(10, 20, 10, 20);

//设置网格对象;

theCollect=[[UICollectionView alloc]initWithFrame:self.view.frame collectionViewLayout:theFlow];

//设置代理;

theCollect.delegate=self;

theCollect.dataSource=self;

//设置为翻整页;

theCollect.pagingEnabled=YES;

//设置背景颜色;

theCollect.backgroundColor=[UIColor lightTextColor];

//添加到视图上;

[self.view addSubview:theCollect];

//注册单元格;

[theCollect registerClass:[myCollectionViewCell class] forCellWithReuseIdentifier:@"mycell"];

//添加页面控制器;

thePage=[[UIPageControl alloc]initWithFrame:CGRectMake(90, 500, 120, 200)];

//设置页面控制器的数目;

thePage.numberOfPages=3;

//设置颜色;

thePage.currentPageIndicatorTintColor=[UIColor redColor];

[self.view addSubview:thePage];

}

//设置网格的分区;

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView

{

return 1;

}

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

{

return 50;

}

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

{

myCollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"mycell" forIndexPath:indexPath] ;

//设置单元格的内容;

cell.theImage.image=[UIImage imageNamed:@"17.jpg"];

cell.theLabel.text=@"信息";

return cell;

}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView

{

thePage.currentPage=theCollect.contentOffset.x/self.view.frame.size.width;

}

创建myCollectionViewCell继承UICollectionViewCell

.h

@property(nonatomic,strong)UIImageView *theImage;

@property(nonatomic,strong)UILabel *theLabel;

.m

//重写单元格;

- (UIImageView *)theImage

{

if (_theImage==nil)

{

_theImage=[[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 50, 50)];

[self addSubview:_theImage];

}

return _theImage;

}

//重写单元格;

- (UILabel *)theLabel

{

if (_theLabel==nil)

{

_theLabel=[[UILabel alloc]initWithFrame:CGRectMake(10, 60, 50, 20)];

[self addSubview:_theLabel];

}

return _theLabel;

}

上一篇下一篇

猜你喜欢

热点阅读