iOS|iOS 自定义CollectionView(自定义View的创建)

//TesteCourseView.h

#import #import "TasteCourseModel.h"@interface TesteCourseView : UIView- (void) setupDataWithTasteCourseModel:(TasteCourseModel *)tasteCourseModel; @end

// TesteCourseView.m
#import "TesteCourseView.h" #import "CourseCollectionViewCell.h"@interface TesteCourseView ()@property (nonatomic, strong)UICollectionView *collectionView; @property (nonatomic, strong)TasteCourseModel *tasteModel; @end@implementation TesteCourseView- (instancetype)init { self = [super init]; if (self) { [self setupView]; } return self; }- (UICollectionView *)collectionView { if (!_collectionView) { UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; layout.scrollDirection = UICollectionViewScrollDirectionVertical; layout.minimumInteritemSpacing = 25/2 *SCALE_WIDTH; layout.itemSize = CGSizeMake(SCREEN_WIDTH - 30 *SCALE_WIDTH, 90 *SCALE_WIDTH); layout.sectionInset = UIEdgeInsetsMake(0, 15 *SCALE_WIDTH, 0, 15 *SCALE_WIDTH); [layout setHeaderReferenceSize:CGSizeMake(0,35 *SCALE_WIDTH)]; _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout]; [_collectionView registerClass:[CourseCollectionViewCell class] forCellWithReuseIdentifier:@"CellReuseIdentifier"]; _collectionView.alwaysBounceVertical = YES; [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerReuseIdentifier"]; _collectionView.showsHorizontalScrollIndicator = NO; _collectionView.backgroundColor = BACKGROUND_COLOR; _collectionView.dataSource = self; _collectionView.delegate = self; } return _collectionView; }- (void) setupDataWithTasteCourseModel:(TasteCourseModel *)tasteCourseModel { self.tasteModel = tasteCourseModel; [self.collectionView reloadData]; }- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{UICollectionReusableView *reusableView =nil; if (kind == UICollectionElementKindSectionHeader) { reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"headerReuseIdentifier" forIndexPath:indexPath]; [(UIView*)[reusableView.subviews lastObject] removeFromSuperview]; UILabel *titleLabel = [[UILabel alloc] init]; titleLabel.text = @"根据学习目标进行推荐"; titleLabel.textColor = NORMARL_FONT_COLOR; titleLabel.font = NORMAL_FONT; [reusableView addSubview:titleLabel]; [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(reusableView.mas_top).offset(5 *SCALE_WIDTH); make.left.mas_equalTo(reusableView).and.offset(20 *SCALE_WIDTH); make.right.mas_equalTo(reusableView).and.offset(-15 *SCALE_WIDTH); make.height.mas_equalTo(reusableView); }]; }return reusableView; }#pragma mark - setupViewAndlayoutView - (void)setupView { [self addSubview:self.collectionView]; [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(self); make.height.mas_equalTo(self); make.left.mas_equalTo(self); make.right.mas_equalTo(self); }]; }#pragma mark - UICollectionViewDataSourceAndUICollectionViewViewDelegate - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return self.tasteModel.tasteCourseArr.count; }/** cell的内容*/ - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {CourseCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CellReuseIdentifier" forIndexPath:indexPath]; [(UIView*)[cell.contentView.subviews lastObject] removeFromSuperview]; NSDictionary *dic = [self.tasteModel.tasteCourseArr objectAtIndex:indexPath.item]; CourseModel *model = [CourseModel mj_objectWithKeyValues:dic]; [cell setupViewWithType:CourseCollectionCellTypeTasteCourse]; [cell setupDataWithModel:model]; return cell; }- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { NSDictionary *dic = [self.tasteModel.tasteCourseArr objectAtIndex:indexPath.item]; CourseModel *model = [CourseModel mj_objectWithKeyValues:dic]; [kNotificationCenter postNotificationName:PUSH_COLLEGE_COURSR_DETAIL object:@(model.Id)]; }

//自定义CollectionCell的创建
//枚举的创建
//CourseCollectionViewCell.h
#import #import "CourseModel.h" @interface CourseCollectionViewCell : UICollectionViewCelltypedef NS_ENUM(NSUInteger, CourseCollectionCellType) { CourseCollectionCellTypeNormal, CourseCollectionCellTypeMyCourse,// 我的课程 CourseCollectionCellTypeCollect,// 收藏的课程 CourseCollectionCellTypeTasteCourse,//体验课程 CourseCollectionCellTypeOrder// 课程订单 }; - (void)setupViewWithType:(CourseCollectionCellType)type; - (void)setupDataWithModel:(CourseModel *)model; @end

【iOS|iOS 自定义CollectionView(自定义View的创建)】//CourseCollectionViewCell.m
#import "CourseCollectionViewCell.h"@interface CourseCollectionViewCell ()@property (nonatomic, strong)UIImageView*imgView; @property (nonatomic, strong)UILabel*titleLabel; @property (nonatomic, strong)UILabel*courseHourLabel; @property (nonatomic, strong)UILabel*learnStateLabel; @property (nonatomic, strong)UILabel*priceLabel; @property (nonatomic, strong)UIView*lineView; @property (nonatomic, strong)UILabel*orderNumberLabel; @property (nonatomic, strong)UILabel*totalPriceLabel; @property (nonatomic, assign)CourseCollectionCellType type; @end@implementation CourseCollectionViewCell#pragma mark - lazyinit - (UIImageView *)imgView { if (!_imgView) { _imgView = [[UIImageView alloc] init]; _imgView.backgroundColor = BACKGROUND_COLOR; } return _imgView; }- (UILabel *)titleLabel { if (!_titleLabel) { _titleLabel = [[UILabel alloc] init]; _titleLabel.numberOfLines = 2; [_titleLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal]; _titleLabel.font = NORMAL_FONT; _titleLabel.textColor = DARK_FONT_COLOR; } return _titleLabel; }- (UILabel *)courseHourLabel { if (!_courseHourLabel) { _courseHourLabel = [[UILabel alloc] init]; _courseHourLabel.font = SMALL_FONT; _courseHourLabel.textColor = NORMARL_FONT_COLOR; } return _courseHourLabel; }- (UILabel *)learnStateLabel { if (!_learnStateLabel) { _learnStateLabel = [[UILabel alloc] init]; _learnStateLabel.font = NORMAL_FONT; _learnStateLabel.textColor = GREEN_COLOR; _learnStateLabel.textAlignment = NSTextAlignmentRight; _learnStateLabel.text = @"继续学习"; } return _learnStateLabel; }- (UILabel *)priceLabel { if (!_priceLabel) { _priceLabel = [[UILabel alloc] init]; _priceLabel.font = NORMAL_FONT; _priceLabel.textColor = GREEN_COLOR; if (self.type != CourseCollectionCellTypeOrder) { _priceLabel.textAlignment = NSTextAlignmentRight; } } return _priceLabel; }- (UIView *)lineView { if (!_lineView) { _lineView = [[UIView alloc] init]; _lineView.backgroundColor = BACKGROUND_COLOR; } return _lineView; }- (UILabel *)orderNumberLabel { if (!_orderNumberLabel) { _orderNumberLabel = [[UILabel alloc] init]; _orderNumberLabel.textColor = NORMARL_FONT_COLOR; _orderNumberLabel.font = LIGHT_FONT; } return _orderNumberLabel; }- (UILabel *)totalPriceLabel { if (!_totalPriceLabel) { _totalPriceLabel = [[UILabel alloc] init]; _totalPriceLabel.textColor = RGBColor(255,90,93); _totalPriceLabel.font = NORMAL_FONT; _totalPriceLabel.textAlignment = NSTextAlignmentRight; } return _totalPriceLabel; }#pragma mark - setupViewAndData - (void)setupViewWithType:(CourseCollectionCellType)type { self.type = type; self.backgroundColor = [UIColor whiteColor]; [self.contentView addSubview:self.imgView]; [self.contentView addSubview:self.titleLabel]; [CommonTool addShadowTo:self]; switch (type) { case CourseCollectionCellTypeNormal: [self.contentView addSubview:self.courseHourLabel]; break; case CourseCollectionCellTypeMyCourse: [self.contentView addSubview:self.courseHourLabel]; [self.contentView addSubview:self.learnStateLabel]; break; case CourseCollectionCellTypeCollect: [self.contentView addSubview:self.courseHourLabel]; [self.contentView addSubview:self.priceLabel]; break; case CourseCollectionCellTypeTasteCourse: [self.contentView addSubview:self.courseHourLabel]; [self.contentView addSubview:self.priceLabel]; break; case CourseCollectionCellTypeOrder: [self.contentView addSubview:self.priceLabel]; [self.contentView addSubview:self.lineView]; [self.contentView addSubview:self.orderNumberLabel]; [self.contentView addSubview:self.totalPriceLabel]; break; } }- (void)setupDataWithModel:(CourseModel *)model { _titleLabel.text = model.courseName; _courseHourLabel.text = [NSString stringWithFormat:@"%ld课时", model.duration]; [_imgView sd_setImageWithURL:[NSURL URLWithString:model.imageUrl] placeholderImage:nil]; _orderNumberLabel.text = [CommonTool putTwoStringTogether:@"订单编号:" and:model.tradeNo]; NSString *priceStr = [NSString stringWithFormat:@"%.2f",model.price]; if (self.type == CourseCollectionCellTypeTasteCourse) { [_priceLabel setText:@"开始体验"]; } else { [_priceLabel setText:[priceStr isEqualToString:@"0.00"] ? @"免费" : [CommonTool putTwoStringTogether:@"¥ " and:priceStr]]; }NSString *str = @"实付: "; self.totalPriceLabel.text = [CommonTool putTwoStringTogether:str and:priceStr]; NSMutableAttributedString *attrDescribeStr = [[NSMutableAttributedString alloc] initWithString:self.totalPriceLabel.text]; [attrDescribeStr addAttribute:NSForegroundColorAttributeName value:NORMARL_FONT_COLOR range:[self.totalPriceLabel.text rangeOfString:str]]; _totalPriceLabel.attributedText = attrDescribeStr; }#pragma mark - layout - (void)layoutSubviews { [_imgView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(self).and.offset(10 *SCALE_WIDTH); make.left.mas_equalTo(self).and.offset(10 *SCALE_WIDTH); make.width.mas_equalTo(125 *SCALE_WIDTH); make.height.mas_equalTo(70 *SCALE_WIDTH); }]; [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(_imgView).and.offset(5 *SCALE_WIDTH); make.left.mas_equalTo(_imgView.mas_right).and.offset(13 *SCALE_WIDTH); make.right.mas_equalTo(self).and.offset(-13 *SCALE_WIDTH); make.height.mas_equalTo(35 *SCALE_WIDTH); }]; if (self.type != CourseCollectionCellTypeOrder) { [_courseHourLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(_titleLabel.mas_bottom).and.offset(10 *SCALE_WIDTH); make.height.mas_equalTo(10 *SCALE_WIDTH); make.left.mas_equalTo(_titleLabel); if (self.type == CourseCollectionCellTypeNormal) { make.right.mas_equalTo(self).and.offset(-15 *SCALE_WIDTH); }else { make.right.mas_equalTo(self).and.offset(-100 *SCALE_WIDTH); } }]; }if (self.type == CourseCollectionCellTypeMyCourse) { [_learnStateLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(_courseHourLabel); make.height.mas_equalTo(10 *SCALE_WIDTH); make.right.mas_equalTo(self).and.offset(-15 *SCALE_WIDTH); make.width.mas_equalTo(60 *SCALE_WIDTH); }]; }if (self.type == CourseCollectionCellTypeCollect || self.type == CourseCollectionCellTypeTasteCourse) { [_priceLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(_courseHourLabel); make.height.mas_equalTo(10 *SCALE_WIDTH); make.right.mas_equalTo(self).and.offset(-15 *SCALE_WIDTH); make.width.mas_equalTo(80 *SCALE_WIDTH); }]; }if (self.type == CourseCollectionCellTypeOrder) { [_priceLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(_titleLabel.mas_bottom).and.offset(10 *SCALE_WIDTH); make.height.mas_equalTo(10 *SCALE_WIDTH); make.left.mas_equalTo(_titleLabel); make.right.mas_equalTo(self).and.offset(-70 *SCALE_WIDTH); }]; [_lineView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(_imgView.mas_bottom).and.offset(10 *SCALE_WIDTH); make.left.mas_equalTo(_imgView); make.right.mas_equalTo(self).and.offset(-10 *SCALE_WIDTH); make.height.mas_equalTo(LINE_PX); }]; [_orderNumberLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(_lineView.mas_bottom); make.left.mas_equalTo(_lineView); make.right.mas_equalTo(self).and.offset(-120 *SCALE_WIDTH); make.bottom.mas_equalTo(self); }]; [_totalPriceLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(_orderNumberLabel); make.right.mas_equalTo(self).and.offset(-10 *SCALE_WIDTH); make.bottom.mas_equalTo(self); make.left.mas_equalTo(_orderNumberLabel.mas_right).and.offset(10 *SCALE_WIDTH); }]; }}@end

    推荐阅读