iOS-集合视图-UIcollectionView
iOS-集合视图-UIcollectionView
//
// ViewController.m
// UI20_UIcollectionView
//
// Created by YIem on 16/3/8.
// Copyright © 2016年 YIem. All rights reserved.
//
import "ViewController.h"
import "CollectionViewCell.h"
@interface ViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
@end
@implementation ViewController
(void)viewDidLoad {
[super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.view.backgroundColor = [UIColor whiteColor]; self.navigationItem.title = @"YIem"; // UICollectionViewFlowLayout 是一个抽象类, 一般使用他的子类 UICollectionViewFlowLayout *flowL= [[[UICollectionViewFlowLayout alloc] init] autorelease]; // 注意 垂直与滚动方向的 是一行 // 注意, 最小- 系统会尽可能的向设置值靠近, 却不会小设置的最小值 // 最小行间距 flowL.minimumLineSpacing = 100; // 最小列间距 flowL.minimumInteritemSpacing = 10; // 单元大小 flowL.itemSize = CGSizeMake(20, 20); // 区头间距 flowL.headerReferenceSize = CGSizeMake(5, 5); // 区脚间距 flowL.footerReferenceSize = CGSizeMake(5, 5); // 与屏幕四边的距离 flowL.sectionInset = UIEdgeInsetsMake(10, 30, 10, 30); // 滚动方向 flowL.scrollDirection = UICollectionViewScrollDirectionHorizontal; // 参数1:界面大小 // 参数2:布局 UICollectionView *collectionV = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:flowL]; collectionV.backgroundColor = [UIColor greenColor]; collectionV.delegate = self; collectionV.dataSource = self; [self.view addSubview:collectionV]; [collectionV release]; // 注册cell(重用) [collectionV registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 100;
} - (UICollectionViewCell )collectionView:(UICollectionView )collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
// cell.backgroundColor = [UIColor redColor];// CollectionViewCell *a = [[CollectionViewCell alloc] init];
cell.label.text =[NSString stringWithFormat:@"%ld", indexPath.row];
return cell;}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
//
// CollectionViewCell.h
// UI20_UIcollectionView
//
// Created by YIem on 16/3/8.
// Copyright © 2016年 YIem. All rights reserved.
//
import <UIKit/UIKit.h>
@interface CollectionViewCell : UICollectionViewCell
- (instancetype)initWithFrame:(CGRect)frame;
- (void)econt;
@property (nonatomic, retain) UILabel *label;
@end
//
// CollectionViewCell.m
// UI20_UIcollectionView
//
// Created by YIem on 16/3/8.
// Copyright © 2016年 YIem. All rights reserved.
//
import "CollectionViewCell.h"
@implementation CollectionViewCell
(instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {[self econt];
}
return self;
}- (void)econt
{
self.label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
_label.backgroundColor = [UIColor cyanColor];
_label.text = @"YIem";
[self.contentView addSubview:_label];
[_label release];
}
@end
当前页面是本站的「Google AMP」版。查看和发表评论请点击:完整版 »