新建HotModel 在hot里屬性比如
@property(nonatomic,copy)NSString*img,*label,*sLabel;
然后新建繼承CollectionViewCell的一個文件在點h里創建屬性
@property(nonatomic,strong)UIImageView *imageView;
@property(nonatomic,strong)UILabel *titleLabel;
@property(nonatomic,strong)UILabel *cedLabel;
@property(nonatomic,strong)HotModel *model;
點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
倒入前兩個頭文件
#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;
}