码迷,mamicode.com
首页 > 移动开发 > 详细

iOS UICollectionView 的使用

时间:2015-05-04 11:58:50      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:ios   uicollectionview   

#define RGBACOLOR(r, g, b, a)   [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:(a)]

#define UserCollectionViewCellID @"RRUserCollectionViewCell"
static NSString *key1 = @"RRHeaderCollectionViewCell";
static NSInteger i = 0;

@interface MyCollectionViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic ,copy) NSString *kind;
@property (nonatomic, strong) NSArray *products;

@end
@implementation MyCollectionViewController
static NSString * const reuseIdentifier = @"Cell";
- (NSArray *)products{
    if (_products == nil || i == 1) {
        
        NSString *path = [[NSBundle mainBundle] pathForResource:@"CollectionViewLict.plist" ofType:nil];
        NSArray *dictArray = [NSArray arrayWithContentsOfFile:path];
        // 将字典转成模型
        NSMutableArray *productArray = [NSMutableArray array];
        for (NSDictionary *dict in dictArray) {
            RRHomePaceSpeedAudienceModel *p = [RRHomePaceSpeedAudienceModel userModelWithDict:dict];
            [productArray addObject:p];
        }
        
        _products = [_products arrayByAddingObjectsFromArray:productArray];
    }
    return _products;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _products = [NSMutableArray array];
    [self configUI];
    UINib *nib = [UINib nibWithNibName:@"RRUserCollectionViewCell" bundle:nil];
    [self.collectionView registerNib:nib forCellWithReuseIdentifier:UserCollectionViewCellID];

    
    [self.collectionView registerNib:[UINib nibWithNibName:@"RRHeaderCollectionViewCell" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"RRHeaderCollectionViewCell"];
    
    
    [self.collectionView registerClass:[self.collectionView.header class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:key1];
    
    
    
    
    self.collectionView.delegate=self;
    self.collectionView.dataSource=self;
}
- (void)configUI{
    __weak typeof(self) weakSelf = self;
    // 添加传统的上拉刷新
    [self.collectionView addLegendFooterWithRefreshingBlock:^{
        [weakSelf loadMoreData];
        // 进入刷新状态后会自动调用这个block
    }];
}
- (void)loadMoreData{
    i = 1;
    self.products = self.products;
    [self.collectionView reloadData];
    [self endRefreshing];
    i = 0;
}
//开始刷新
- (void)beginRefreshing{
    [self.collectionView.footer beginRefreshing];
}
//结束刷新
-(void)endRefreshing{
    [self.collectionView.footer endRefreshing];
}
- (id)init{
    // 1.流水布局
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    
    // 2.每个cell的尺寸
    layout.itemSize = CGSizeMake(60, 60);
    // 3.设置cell之间的水平间距
    layout.minimumInteritemSpacing = 40;
    // 4.设置cell之间的垂直间距
    layout.minimumLineSpacing =20;
    // 5.设置四周的内边距
    layout.sectionInset = UIEdgeInsetsMake(layout.minimumLineSpacing,layout.minimumInteritemSpacing, layout.minimumLineSpacing,layout.minimumInteritemSpacing);
    return [super initWithCollectionViewLayout:layout];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark <UICollectionViewDataSource> header
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeMake([UIScreen mainScreen].bounds.size.width, 154);//宽默认
    
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.products.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    // 1.获得cell
    RRUserCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:UserCollectionViewCellID forIndexPath:indexPath];
    // 2.传递模型
    cell.userModel = self.products[indexPath.item];
    return cell;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    [self.collectionView registerNib:[UINib nibWithNibName:@"RRHeaderCollectionViewCell" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"RRHeaderCollectionViewCell"];
    UICollectionReusableView *view =  [self.collectionView dequeueReusableSupplementaryViewOfKind :kind withReuseIdentifier:@"RRHeaderCollectionViewCell" forIndexPath:indexPath];
    return view;
}

#pragma mark <UICollectionViewDelegate>
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    [super touchesEnded:touches withEvent:event];
}

iOS UICollectionView 的使用

标签:ios   uicollectionview   

原文地址:http://blog.csdn.net/darongzi1314/article/details/45477855

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!