2018-05-04

新建HotModel 在hot里屬性比如

@property(nonatomic,copy)NSString*img,*label,*sLabel;


然后新建繼承CollectionViewCell的一個(gè)文件在點(diǎn)h里創(chuàng)建屬性

@property(nonatomic,strong)UIImageView *imageView;

@property(nonatomic,strong)UILabel *titleLabel;

@property(nonatomic,strong)UILabel *cedLabel;

@property(nonatomic,strong)HotModel *model;

點(diǎn)m

-(instancetype)initWithFrame:(CGRect)frame{

? ? self = [super initWithFrame:frame];

? ? if (self) {

? ? ? ? _imageView = [[UIImageView alloc] init];

? ? ? ? [self.contentView addSubview:_imageView];

? ? ? ? _titleLabel = [[UILabel alloc] init];

? ? ? ? [self.contentView addSubview:_titleLabel];

? ? ? ? _cedLabel = [[UILabel alloc] init];

? ? ? ? [self.contentView addSubview:_cedLabel];

? ? }

? ? return self;

}

-(void)layoutSubviews{

? ? _imageView.frame = CGRectMake(10, 10, SCREEN_RATE_640_WIDTH(60), SCREEN_RATE_640_WIDTH(60));

? ? _titleLabel.frame = CGRectMake(VIEW_RIGHT(_imageView)+10, VIEW_Y(_imageView), Contenview_Width - 30 - SCREEN_RATE_640_WIDTH(60), VIEW_HEIGHT(_imageView));

? ? _cedLabel.frame = CGRectMake(VIEW_X(_imageView), VIEW_BUTTOM(_imageView) +10, Contenview_Width - 20, Contenview_Height - 40 - SCREEN_RATE_640_WIDTH(60));

? ? _cedLabel.numberOfLines = 0;

}

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

? ? _imageView.image = [UIImage imageNamed:model.img];

? ? _titleLabel.text = model.label;

? ? _cedLabel.text = model.sLabel;

}

在viewControllr

倒入前兩個(gè)頭文件

#import "ViewController.h"#import "define.h"#import "HotCollectCollectionViewCell.h"#import "HotModel.h"@interface ViewController ()@property(nonatomic,strong)UICollectionView *collectionView;

@end

@implementation ViewController

-(void)collectionview{

? ? UICollectionViewFlowLayout *flowlayout = [[UICollectionViewFlowLayout alloc] init];

? ? flowlayout.itemSize = CGSizeMake((SCREEN_WIDTH - 30)/2.0, (SCREEN_WIDTH - 30)/2.0 *4/5);

? ? flowlayout.scrollDirection = UICollectionViewScrollDirectionVertical;

? ? flowlayout.minimumLineSpacing = 10;

? ? flowlayout.minimumInteritemSpacing = 10;

? ? _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT- 64 - 49) collectionViewLayout:flowlayout];

_collectionView.backgroundColor = [UIColor whiteColor];

? ? _collectionView.delegate = self;

? ? _collectionView.dataSource = self;

? ? [_collectionView registerClass:[HotCollectCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];

? ? [self.view addSubview:_collectionView];

}

- (void)viewDidLoad {

? ? [super viewDidLoad];

? ? self.view.backgroundColor = [UIColor lightGrayColor];


? ? [self collectionview];

}

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

? ? return 20;

}

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

? ? static NSString *str = @"cell";

? ? HotCollectCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:str forIndexPath:indexPath];

? ? if (!cell) {

? ? ? ? cell = [[HotCollectCollectionViewCell alloc] init];

? ? }

? ? HotModel *model = [[HotModel alloc] init];

? ? model.img = @"index";

? ? model.label = @"bawei";

? ? model.sLabel = @"baweibaweibaweibaweibaweibaweibaweibaweibaweibaweibawei";

? ? [cell setModel:model];

? ? return cell;

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容