在.h中声明
创新互联专注于企业成都全网营销推广、网站重做改版、石嘴山网站定制设计、自适应品牌网站建设、H5场景定制、商城建设、集团公司官网建设、外贸营销网站建设、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为石嘴山等各大城市提供网站开发制作服务。@property (nonatomic, strong) UICollectionView *myCollecionView;
遵循CollectionView协议
.m文件中实现
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_collectionView.delegate = self;
_collectionView.dataSource = self;
//创建布局对象
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
//itme与item之间的最小间距--默认是10
// flowLayout.minimumInteritemSpacing = 110;
// flowLayout.minimumLineSpacing = 100;
// flowLayout.itemSize = CGSizeMake(80, 80);
// flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
//初始化CollectionView
_myCollecionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 667 - 284, 375, 284) collectionViewLayout:flowLayout];
_myCollecionView.tag = 200;
_myCollecionView.delegate = self;
_myCollecionView.dataSource = self;
_myCollecionView.backgroundColor = [UIColor redColor];
[self.view addSubview:_myCollecionView];
//注册单元格
[_myCollecionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"myCollecionViewCell"];
}
#pragma mark -UICollectionViewDataSource
//指定组的个数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 2;
}
//指定单元格的个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 21;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
if (collectionView.tag == 100) {
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionViewCell" forIndexPath:indexPath];
cell.backgroundColor = [UIColor colorWithRed:arc4random() % 10 * 0.1 green:arc4random() % 10 * 0.1 blue:arc4random() % 10 * 0.1 alpha:1];
return cell;
}else if (collectionView.tag == 200) {
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"myCollecionViewCell" forIndexPath:indexPath];
cell.backgroundColor = [UIColor colorWithRed:arc4random() % 10 * 0.1 green:arc4random() % 10 * 0.1 blue:arc4random() % 10 * 0.1 alpha:1];
return cell;
}
return nil;
}
//动态地设置单元格的尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(80, arc4random() % 80);
}
@end
创新互联www.cdcxhl.cn,专业提供香港、美国云服务器,动态BGP最优骨干路由自动选择,持续稳定高效的网络助力业务部署。公司持有工信部办法的idc、isp许可证, 机房独有T级流量清洗系统配攻击溯源,准确进行流量调度,确保服务器高可用性。佳节活动现已开启,新人活动云服务器买多久送多久。
本文标题:UI中CollectionView的创建与使用-创新互联
网页地址:http://lswzjz.com/article/ccsjpd.html