美文网首页
UICollectionView

UICollectionView

作者: 古月思吉 | 来源:发表于2019-07-24 17:57 被阅读0次
效果图.png

1、MHConnectDeviceTipsView.h 文件:

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface MHConnectDeviceTipsView : UIView

@property (nonatomic, copy) NSArray * dataSource;//数据源

@end

NS_ASSUME_NONNULL_END

2、MHConnectDeviceTipsView.m 文件:

#import "MHConnectDeviceTipsView.h"
#import "MHConnectDeviceTipsCell.h"
#import "MHConnectDeviceTipsModel.h"

@interface MHConnectDeviceTipsView ()<UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UIImageView * bgImageView;//背景图
@property (nonatomic, strong) UICollectionView * collectionView;

@end

@implementation MHConnectDeviceTipsView

#pragma mark - Getter

- (UIImageView *)bgImageView {
    if (!_bgImageView) {
        _bgImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pic_tips"]];
        _bgImageView.contentMode = UIViewContentModeScaleAspectFit;
        _bgImageView.frame = CGRectMake(self.bounds.size.width-103, self.bounds.size.height-130, 103, 130);
    }
    return _bgImageView;
}

- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc] init];
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:layout];
        _collectionView.pagingEnabled = YES;
        _collectionView.backgroundColor = [UIColor clearColor];
        _collectionView.showsHorizontalScrollIndicator = NO;
        [_collectionView registerClass:[MHConnectDeviceTipsCell class] forCellWithReuseIdentifier:@"MHConnectDeviceTipsCell"];
    }
    return _collectionView;
}


#pragma mark - Setter

- (void)setDataSource:(NSArray *)dataSource {
    _dataSource = dataSource;
    [self.collectionView reloadData];
}


#pragma mark - Life Cycle

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self prepareUI];
    }
    return self;
}


#pragma mark - UI

- (void)prepareUI {
    self.backgroundColor = [UIColor whiteColor];
    self.layer.cornerRadius = 8.0;
    self.layer.masksToBounds = YES;
    
    [self addSubview:self.bgImageView];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self addSubview:self.collectionView];
    
    //配置【固定】数据源
    NSArray * describesArray = @[NeuLocalizedString(@"If your plans changs, you can adjust your AC's schedule for that day"), NeuLocalizedString(@"Your AC can automatically adjust the temperature to your preferred setting while you sleep. Saving your energy and keeping you comfortable."), NeuLocalizedString(@"You can share your connected device to your family members or friends to control at the same time.")];
    NSMutableArray * modelsArray = [NSMutableArray new];
    for (NSString * describeStr in describesArray) {
        MHConnectDeviceTipsModel * model = [[MHConnectDeviceTipsModel alloc] init];
        model.title = NeuLocalizedString(@"Did you know?");
        model.describe = describeStr;
        [modelsArray addObject:model];
    }
    self.dataSource = modelsArray;
}


#pragma mark - UICollectionViewDataSource, UICollectionViewDelegateFlowLayout

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataSource.count;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    MHConnectDeviceTipsCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MHConnectDeviceTipsCell" forIndexPath:indexPath];
    cell.model = (MHConnectDeviceTipsModel *)self.dataSource[indexPath.row];
    return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(self.bounds.size.width, self.bounds.size.height);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 0;
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(0, 0, 0, 0);
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
}

@end

3、MHConnectDeviceTipsCell.h 文件:

#import <UIKit/UIKit.h>
#import "MHConnectDeviceTipsModel.h"

NS_ASSUME_NONNULL_BEGIN

@interface MHConnectDeviceTipsCell : UICollectionViewCell

@property (nonatomic, strong) MHConnectDeviceTipsModel * model;

@end

NS_ASSUME_NONNULL_END

4、MHConnectDeviceTipsCell.m 文件:

#import "MHConnectDeviceTipsCell.h"

@interface MHConnectDeviceTipsCell ()

@property (nonatomic, strong) UILabel * titleLabel;//标题
@property (nonatomic, strong) UILabel * describeLabel;//说明

@end

@implementation MHConnectDeviceTipsCell

#pragma mark - Getter

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.textColor = [UIColor blackColor];
        _titleLabel.font = MEDIUMSYSTEMFONT(16);
        _titleLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _titleLabel;
}

- (UILabel *)describeLabel {
    if (!_describeLabel) {
        _describeLabel = [[UILabel alloc] init];
        _describeLabel.textColor = COLOR_TEXT_THIRDLEVEL;
        _describeLabel.font = SYSTEMFONT(14);
        _describeLabel.textAlignment = NSTextAlignmentCenter;
        _describeLabel.numberOfLines = 0;
    }
    return _describeLabel;
}


#pragma mark - Setter

- (void)setModel:(MHConnectDeviceTipsModel *)model {
    _model = model;
    
    self.titleLabel.text = _model.title;
    self.describeLabel.text = _model.describe;
}


#pragma mark - Life Cycle

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self prepareUI];
    }
    return self;
}


#pragma mark - UI

- (void)prepareUI {
    self.contentView.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:self.titleLabel];
    [self.contentView addSubview:self.describeLabel];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(24);
        make.left.equalTo(self.contentView).offset(32);
        make.right.equalTo(self.contentView).offset(-32);
        make.height.mas_equalTo(20);
    }];
    [self.describeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).offset(10);
        make.left.equalTo(self.contentView).offset(32);
        make.right.equalTo(self.contentView).offset(-32);
        make.bottom.lessThanOrEqualTo(self.contentView.mas_bottom).offset(-24);
    }];
}

@end

5、MHConnectDeviceTipsModel.h 文件:

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface MHConnectDeviceTipsModel : NSObject

@property (nonatomic, copy) NSString * title;//标题
@property (nonatomic, copy) NSString * describe;//描述

@end

NS_ASSUME_NONNULL_END

相关文章

网友评论

      本文标题:UICollectionView

      本文链接:https://www.haomeiwen.com/subject/fajfrctx.html