iOS 自定义UICollectionViewCell

来源:互联网 发布:桌面图标美化软件 编辑:程序博客网 时间:2024/05/18 14:13


@property (nonatomic,strong) UICollectionView *myCollectionView;


#pragma mark - UICollectionView

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

    

    returnself.dataArray.count;

}

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

    

    GoodCollectionViewCell * cell = [collectionViewdequeueReusableCellWithReuseIdentifier:@"ID"forIndexPath:indexPath];

    

    cell.model =self.dataArray[indexPath.row];

    

    return cell;

}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{

    

    returnCGSizeMake((WIDTH-60)/2, (WIDTH-60)/2+50);

}


- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{


    returnUIEdgeInsetsMake(5,20, 0.01,20);

}


- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{

    

    return5;

}


- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{

    

    return20;

}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

    

    GoodDetailViewController *gv = [[GoodDetailViewControlleralloc] init];

    gv.model =self.dataArray[indexPath.row];

    [self.navigationControllerpushViewController:gv animated:YES];

    [collectionView reloadItemsAtIndexPaths:@[indexPath]];

}


- (UICollectionView *)myCollectionView{


    if (_myCollectionView ==nil) {

        

        UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayoutalloc]init];

        layout.scrollDirection =UICollectionViewScrollDirectionVertical;

        

        _myCollectionView = [[UICollectionViewalloc]initWithFrame:CGRectMake(0,64, WIDTH, HEIGHT-64-49)collectionViewLayout:layout];

        _myCollectionView.backgroundColor = [UIColorclearColor];

        

        _myCollectionView.delegate =self;

        _myCollectionView.dataSource =self;

        

        [_myCollectionViewregisterClass:[GoodCollectionViewCellclass] forCellWithReuseIdentifier:@"ID"];

        _myCollectionView.hidden =YES;

        

        _myCollectionView.mj_header = [MJRefreshNormalHeaderheaderWithRefreshingTarget:selfrefreshingAction:@selector(headRefresh)];

        _myCollectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:selfrefreshingAction:@selector(footerRefresh)];

        

    }

    

    return_myCollectionView;

}


#pragma mark - 下拉刷新

- (void)headRefresh{

    

    self.row =0;

    

    [selfgetData];

    

}


- (void)footerRefresh{

    

    self.row +=10;

    [selfgetData];

}



#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 = [superinitWithFrame:frame];

    if (self) {

        

        self.backgroundColor = [UIColorwhiteColor];

        

        [selfcreateUI];

    }

    returnself;

}

- (void)createUI{

    

    iconView = [[UIImageViewalloc] initWithFrame:CGRectMake(10,10, cellW-20,cellW-20)];

    iconView.backgroundColor = [UIColorclearColor];

    [self.contentViewaddSubview:iconView];

    

    titleLabel = [[UILabelalloc] initWithFrame:CGRectMake(10,CGRectGetMaxY(iconView.frame),cellW-20,20)];

    titleLabel.textColor =selectColor;

    titleLabel.font = [UIFontsystemFontOfSize:13.0f];

    [self.contentViewaddSubview:titleLabel];

    

    priceLabel = [[UILabelalloc] initWithFrame:CGRectMake(10,CGRectGetMaxY(titleLabel.frame),cellW-20,20)];

    priceLabel.textColor = [UIColorgrayColor];

    priceLabel.font = [UIFontsystemFontOfSize:13.0f];

    [self.contentViewaddSubview:priceLabel];

    

    addressLabel = [[UILabelalloc] initWithFrame:CGRectMake(10,CGRectGetMaxY(priceLabel.frame),cellW-20,20)];

    addressLabel.textColor = [UIColorgrayColor];

    addressLabel.font = [UIFontsystemFontOfSize:10.0f];

    [self.contentViewaddSubview:addressLabel];

    

}


- (void)setModel:(GoodModel *)model{

    

    _model = model;

    

    if ([model.stateintegerValue] == 1) {

        

        if ([StringIsNullisNotBlankString:model.image]) {

            

            [iconViewsd_setImageWithURL:[NSURLURLWithString:model.image]placeholderImage:[UIImageimageNamed:@"chaohuitechan"]];

        }else{

            

            iconView.image = [UIImageimageNamed:@"chaohuitechan"];

        }

    }else{

        

        iconView.image = [UIImageimageNamed:@"stop"];

    }

    

    titleLabel.text = [NSStringstringWithFormat:@"%@",model.title];

    priceLabel.text = [NSStringstringWithFormat:@"%@元/斤",model.price];

    addressLabel.text = [NSStringstringWithFormat:@"%@",model.address];

    

}


@end





0 0