您的位置:首页 > 产品设计 > UI/UE

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息