您的位置:首页 > 其它

collectionView(xib)

2016-06-04 21:57 381 查看
 //

//  RootViewController.m

//  UI-day12-XIB使用UICollectionView

//

//  Created by liufy on 15/8/4.

//  Copyright (c) 2015年 liufy. All rights reserved.

//

//用xib使用了注册,就不用再在xib中打上复用标识了

#import "RootViewController.h"

#import "Stamp.h"

#import "MyCollectionViewCell.h"

#import "MyHeaderView.h"

//注册使用的nib文件名

#define KMyCollectionViewCell @"MyCollectionViewCell"

#define KMyHeaderView @"MyHeaderView"

//复用标示

#define KCell @"cell"

#define KHeaderView @"headerView"

@interface RootViewController ()<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (weak, nonatomic) IBOutlet UICollectionView *myCollectionView;

@property (nonatomic,strong)NSMutableArray *dataArr;//数据源数组

@end

@implementation RootViewController

-(NSMutableArray *)dataArr{

    if (!_dataArr) {

        _dataArr = [NSMutableArray new];

    }

    return _dataArr;

    

}

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view from its nib.

    //注册cell

    [self.myCollectionView registerNib:[UINib nibWithNibName:KMyCollectionViewCell bundle:nil] forCellWithReuseIdentifier:KCell];

    //注册头部视图

    [self.myCollectionView registerNib:[UINib nibWithNibName:KMyHeaderView bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:KHeaderView];

    //注册尾部视图

    [self.myCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footerView"];

    

    self.myCollectionView.backgroundColor =
[UIColor whiteColor];

    

    [self loadData];

    

}

//装载数据

-(void)loadData{

    

    for (NSInteger i = 0;i < 10 ; i++) {

        

        Stamp *stampModel = [Stamp new];

        

        stampModel.priceStr = [NSString stringWithFormat:@"¥%d",arc4random()%100];

        stampModel.stampImageNameStr = [NSString stringWithFormat:@"%ld.jpg", i + 1];

        

        [self.dataArr addObject:stampModel];

    }

    //刷新UI

    [self.myCollectionView reloadData];

    

}

#pragma mark- UICollectionView的代理方法

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

    

    return 10;

    

}

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

    

    MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:KCell forIndexPath:indexPath];

    //刷新UI

    [cell refreshUI:self.dataArr[indexPath.row]];

    

    return cell;

    

}

//返回头部和尾部视图的UICollectionReusableView对象

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind
atIndexPath:(NSIndexPath *)indexPath{

    //根据kind判断需要返回的头部还是尾部视图

    if ([kind isEqualToString:UICollectionElementKindSectionHeader])
{

        //头部视图使用自定义头不视图MyHeaderView

        MyHeaderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:KHeaderView forIndexPath:indexPath];

        

        return headerView;

    }else{

        //尾部视图使用系统的类UICollectionReusableView

        UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footerView" forIndexPath:indexPath];

        footerView.backgroundColor = [UIColor yellowColor];

        return footerView;

    }

    

}

@end

//

//  MyCollectionViewCell.m

//  UI-day12-XIB使用UICollectionView

//

//  Created by liufy on 15/8/4.

//  Copyright (c) 2015年 liufy. All rights reserved.

//

#import "MyCollectionViewCell.h"

@interface MyCollectionViewCell ()

@property (weak, nonatomic) IBOutlet UIImageView *bgView;//背景图

@property (weak, nonatomic) IBOutlet UILabel *priceLabel;//价格

@end

@implementation MyCollectionViewCell

- (void)awakeFromNib {

    //awakeFromNib 相当于手写代码的初始化方法(XIB定制cell)

    //设置self.bgView的显示模式

    //UIViewContentModeScaleAspectFill 等比例拉伸,可以超出视图的范围

    self.bgView.contentMode = UIViewContentModeScaleAspectFill;

    //切除超出视图的部分

    self.bgView.clipsToBounds = YES;

    

}

-(void)refreshUI:(Stamp *)stampModel{

    

    self.priceLabel.text = stampModel.priceStr;

    self.bgView.image = [UIImage imageNamed:stampModel.stampImageNameStr];

    

    

    

    

}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: