iOS 自定义UICollectionViewCell
2016-12-27 08:44
555 查看
@property (nonatomic,
strong) UICollectionView *myCollectionView;
#pragma mark - UICollectionView
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return
self.dataArray.count;
}
- (UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath
*)indexPath{
GoodCollectionViewCell * cell = [collectionView
dequeueReusableCellWithReuseIdentifier:@"ID"
forIndexPath:indexPath];
cell.model =
self.dataArray[indexPath.row];
return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout
*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
return
CGSizeMake((WIDTH-60)/2, (WIDTH-60)/2+50);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout
*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
return
UIEdgeInsetsMake(5,
20,
0.01,20);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout
*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
return
5;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout
*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
return
20;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath
*)indexPath{
GoodDetailViewController *gv = [[GoodDetailViewController
alloc]
init];
gv.model =
self.dataArray[indexPath.row];
[self.navigationController
pushViewController:gv
animated:YES];
[collectionView reloadItemsAtIndexPaths:@[indexPath]];
}
- (UICollectionView *)myCollectionView{
if (_myCollectionView ==
nil) {
UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout
alloc]init];
layout.scrollDirection =
UICollectionViewScrollDirectionVertical;
_myCollectionView = [[UICollectionView
alloc]initWithFrame:CGRectMake(0,
64,
WIDTH, HEIGHT-64-49)
collectionViewLayout:layout];
_myCollectionView.backgroundColor = [UIColor
clearColor];
_myCollectionView.delegate =
self;
_myCollectionView.dataSource =
self;
[_myCollectionView
registerClass:[GoodCollectionViewCell
class] forCellWithReuseIdentifier:@"ID"];
_myCollectionView.hidden =
YES;
_myCollectionView.mj_header = [MJRefreshNormalHeader
headerWithRefreshingTarget:self
refreshingAction:@selector(headRefresh)];
_myCollectionView.mj_footer = [MJRefreshAutoNormalFooter
footerWithRefreshingTarget:self
refreshingAction:@selector(footerRefresh)];
}
return
_myCollectionView;
}
#pragma mark - 下拉刷新
- (void)headRefresh{
self.row =
0;
[self
getData];
}
- (void)footerRefresh{
self.row +=
10;
[self
getData];
}
#import <UIKit/UIKit.h>
#import "GoodModel.h"
@interface GoodCollectionViewCell :
UICollectionViewCell
@property (nonatomic,
strong) UIImageView *iconView;
@property (nonatomic,
strong) UILabel *titleLabel;
@property (nonatomic,
strong) UILabel *priceLabel;
@property (nonatomic,
strong) UILabel *addressLabel;
@property (nonatomic,
strong) GoodModel *model;
@end
#import "GoodCollectionViewCell.h"
#import "Header.h"
# define cellW self.frame.size.width
# define cellH self.frame.size.height
@implementation GoodCollectionViewCell
@synthesize iconView,titleLabel,priceLabel,addressLabel;
- (id)initWithFrame:(CGRect)frame{
self = [super
initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor
whiteColor];
[self
createUI];
}
return
self;
}
- (void)createUI{
iconView = [[UIImageView
alloc] initWithFrame:CGRectMake(10,
10,
cellW-20,
cellW-20)];
iconView.backgroundColor = [UIColor
clearColor];
[self.contentView
addSubview:iconView];
titleLabel = [[UILabel
alloc] initWithFrame:CGRectMake(10,
CGRectGetMaxY(iconView.frame),
cellW-20,
20)];
titleLabel.textColor =
selectColor;
titleLabel.font = [UIFont
systemFontOfSize:13.0f];
[self.contentView
addSubview:titleLabel];
priceLabel = [[UILabel
alloc] initWithFrame:CGRectMake(10,
CGRectGetMaxY(titleLabel.frame),
cellW-20,
20)];
priceLabel.textColor = [UIColor
grayColor];
priceLabel.font = [UIFont
systemFontOfSize:13.0f];
[self.contentView
addSubview:priceLabel];
addressLabel = [[UILabel
alloc] initWithFrame:CGRectMake(10,
CGRectGetMaxY(priceLabel.frame),
cellW-20,
20)];
addressLabel.textColor = [UIColor
grayColor];
addressLabel.font = [UIFont
systemFontOfSize:10.0f];
[self.contentView
addSubview:addressLabel];
}
- (void)setModel:(GoodModel *)model{
_model = model;
if ([model.state
integerValue] ==
1) {
if ([StringIsNull
isNotBlankString:model.image]) {
[iconView
sd_setImageWithURL:[NSURL
URLWithString:model.image]
placeholderImage:[UIImage
imageNamed:@"chaohuitechan"]];
}else{
iconView.image = [UIImage
imageNamed:@"chaohuitechan"];
}
}else{
iconView.image = [UIImage
imageNamed:@"stop"];
}
titleLabel.text = [NSString
stringWithFormat:@"%@",model.title];
priceLabel.text = [NSString
stringWithFormat:@"%@
元/斤",model.price];
addressLabel.text = [NSString
stringWithFormat:@"%@",model.address];
}
@end
strong) UICollectionView *myCollectionView;
#pragma mark - UICollectionView
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return
self.dataArray.count;
}
- (UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath
*)indexPath{
GoodCollectionViewCell * cell = [collectionView
dequeueReusableCellWithReuseIdentifier:@"ID"
forIndexPath:indexPath];
cell.model =
self.dataArray[indexPath.row];
return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout
*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
return
CGSizeMake((WIDTH-60)/2, (WIDTH-60)/2+50);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout
*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
return
UIEdgeInsetsMake(5,
20,
0.01,20);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout
*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
return
5;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout
*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
return
20;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath
*)indexPath{
GoodDetailViewController *gv = [[GoodDetailViewController
alloc]
init];
gv.model =
self.dataArray[indexPath.row];
[self.navigationController
pushViewController:gv
animated:YES];
[collectionView reloadItemsAtIndexPaths:@[indexPath]];
}
- (UICollectionView *)myCollectionView{
if (_myCollectionView ==
nil) {
UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout
alloc]init];
layout.scrollDirection =
UICollectionViewScrollDirectionVertical;
_myCollectionView = [[UICollectionView
alloc]initWithFrame:CGRectMake(0,
64,
WIDTH, HEIGHT-64-49)
collectionViewLayout:layout];
_myCollectionView.backgroundColor = [UIColor
clearColor];
_myCollectionView.delegate =
self;
_myCollectionView.dataSource =
self;
[_myCollectionView
registerClass:[GoodCollectionViewCell
class] forCellWithReuseIdentifier:@"ID"];
_myCollectionView.hidden =
YES;
_myCollectionView.mj_header = [MJRefreshNormalHeader
headerWithRefreshingTarget:self
refreshingAction:@selector(headRefresh)];
_myCollectionView.mj_footer = [MJRefreshAutoNormalFooter
footerWithRefreshingTarget:self
refreshingAction:@selector(footerRefresh)];
}
return
_myCollectionView;
}
#pragma mark - 下拉刷新
- (void)headRefresh{
self.row =
0;
[self
getData];
}
- (void)footerRefresh{
self.row +=
10;
[self
getData];
}
#import <UIKit/UIKit.h>
#import "GoodModel.h"
@interface GoodCollectionViewCell :
UICollectionViewCell
@property (nonatomic,
strong) UIImageView *iconView;
@property (nonatomic,
strong) UILabel *titleLabel;
@property (nonatomic,
strong) UILabel *priceLabel;
@property (nonatomic,
strong) UILabel *addressLabel;
@property (nonatomic,
strong) GoodModel *model;
@end
#import "GoodCollectionViewCell.h"
#import "Header.h"
# define cellW self.frame.size.width
# define cellH self.frame.size.height
@implementation GoodCollectionViewCell
@synthesize iconView,titleLabel,priceLabel,addressLabel;
- (id)initWithFrame:(CGRect)frame{
self = [super
initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor
whiteColor];
[self
createUI];
}
return
self;
}
- (void)createUI{
iconView = [[UIImageView
alloc] initWithFrame:CGRectMake(10,
10,
cellW-20,
cellW-20)];
iconView.backgroundColor = [UIColor
clearColor];
[self.contentView
addSubview:iconView];
titleLabel = [[UILabel
alloc] initWithFrame:CGRectMake(10,
CGRectGetMaxY(iconView.frame),
cellW-20,
20)];
titleLabel.textColor =
selectColor;
titleLabel.font = [UIFont
systemFontOfSize:13.0f];
[self.contentView
addSubview:titleLabel];
priceLabel = [[UILabel
alloc] initWithFrame:CGRectMake(10,
CGRectGetMaxY(titleLabel.frame),
cellW-20,
20)];
priceLabel.textColor = [UIColor
grayColor];
priceLabel.font = [UIFont
systemFontOfSize:13.0f];
[self.contentView
addSubview:priceLabel];
addressLabel = [[UILabel
alloc] initWithFrame:CGRectMake(10,
CGRectGetMaxY(priceLabel.frame),
cellW-20,
20)];
addressLabel.textColor = [UIColor
grayColor];
addressLabel.font = [UIFont
systemFontOfSize:10.0f];
[self.contentView
addSubview:addressLabel];
}
- (void)setModel:(GoodModel *)model{
_model = model;
if ([model.state
integerValue] ==
1) {
if ([StringIsNull
isNotBlankString:model.image]) {
[iconView
sd_setImageWithURL:[NSURL
URLWithString:model.image]
placeholderImage:[UIImage
imageNamed:@"chaohuitechan"]];
}else{
iconView.image = [UIImage
imageNamed:@"chaohuitechan"];
}
}else{
iconView.image = [UIImage
imageNamed:@"stop"];
}
titleLabel.text = [NSString
stringWithFormat:@"%@",model.title];
priceLabel.text = [NSString
stringWithFormat:@"%@
元/斤",model.price];
addressLabel.text = [NSString
stringWithFormat:@"%@",model.address];
}
@end
相关文章推荐
- iOS UICollectionView 入门 05 创建自定义UICollectionViewCell
- IOS菜鸟的所感所思(四)——自定义UICollectionViewCell以及网络数据的添加。
- iOS开发:xib之UICollectionView自定义cell
- [IOS]swift自定义uicollectionviewcell
- 20141211笔记(UIImageView 设置内容的Mode的方法\UICollectionViewCell Custom的方法\ios modal segue code)
- IOS之UITableView cell自定义视图中插入Tableview
- 使用纯代码定义UICollectionView和自定义UICollectionViewCell
- IOS开发之TableView、多个TableViewCell、自定义Cell、Cell上画画(故事板+代码方式)
- iOS 使用MJRefrsh上拉加载,下拉刷新cell不充满UICollectionView时不能刷新的问题
- swift UICollectionView 代码创建 和自定义Cell
- IOS UITableView 移动自定义cell
- UICollectionViewController xcode6.1 自定义Cell
- Ios 自定义表示图出现的相关问题UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'
- iOS开发技巧-tableView去掉多余的空行分割线,自定义Cell分割线
- 【iOS开发TableView】TabelView自定义cell
- iOS自定义UITableView的cell
- IOS瀑布流 通过自定义UICollectionViewController的Layout布局实现
- iOS开发UITableView 之代码自定义cell的步骤
- ios UITableview自定义cell
- 【iOS】UICollectionView自定义Layout之蜂窝布局