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
// 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
相关文章推荐
- 面试题1:类的赋值运算符函数
- 设计模式学习(4)
- 学习笔记之一:学习中好的试题
- Lowest Common Ancestor of a Binary Search Tree
- 游戏编程中的人工智能-遗传算法入门(五)
- Android Studio1.5使用和学习记录
- html列表相关属性
- LeetCode 76. Minimum Window Substring
- Apriori算法的python实现
- Log4j2与JNDI结合
- C++ string类型变量
- JAVA-WEB开发第五讲[2016-06-04]SPRING-MVC环境搭建
- collectionView(手写)
- collectionView(手写)
- 人月神话阅读笔记2
- 赢在职场-WEB全栈开发-1-9 文本样式概览
- 在mac osx 下使用 Jenkins对Android 进行持续集成
- CentOS 6.x RHCS Cluster 配置
- css美化number类型输入框美化实现自定义+、-号点击按钮增减效果
- java上转型的多态