UICollectionView集合视图

2020-11-07  本文已影响0人  我不白先生

AppDelegate.m

#import "AppDelegate.h"
#import "CollectionViewController.h"
#import "MyLayout.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.window.rootViewController = [[CollectionViewController alloc]initWithCollectionViewLayout:[[MyLayout alloc]init]];
    [self.window makeKeyAndVisible];
    return YES;
}

CollectionViewController.m

#import "CollectionViewController.h"
#import "MyCell.h"
@interface CollectionViewController ()

@end

@implementation CollectionViewController

static NSString * const reuseIdentifier = @"Cell";

- (void)viewDidLoad {
    [super viewDidLoad];
    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = NO;
    // Register cell classes系统自动帮我们注册了
    [self.collectionView registerClass:[MyCell class] forCellWithReuseIdentifier:reuseIdentifier];
     self.collectionView.pagingEnabled = YES;
    // Do any additional setup after loading the view.
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {

    return 3;
}

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

    return 9;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    MyCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    // Configure the cell
    cell.backgroundColor = [UIColor redColor];
    UILabel *label = [cell.contentView viewWithTag:100];
    if(!label){
    label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 80, 80)];
    label.tag = 100;
    label.text = [NSString stringWithFormat:@"%ld",indexPath.item];
    label.font = [UIFont systemFontOfSize:30];
    [cell.contentView addSubview:label];}
    cell.imageView.image = [UIImage imageNamed:@"心动"];
    cell.label.text = [NSString stringWithFormat:@"%ld/%ld",indexPath.item,indexPath.section];
    cell.backgroundImageView.image = [UIImage imageNamed:@"1"];
    return cell;
}

MyLayout.m

@implementation MyLayout
-(instancetype)init{
    if (self = [super init] ) {
        //设置每项大小
        self.itemSize = CGSizeMake(80, 80);
        //设置最小行距
        self.minimumLineSpacing = 10;
        //设置最小项间距
        self.minimumInteritemSpacing = 10;
        //设置 分区中的内边距
        self.sectionInset = UIEdgeInsetsMake(238, 77, 238, 77);
        //设置滚动方向
        self.scrollDirection = UICollectionViewScrollDirectionVertical;
        //设置分区头和分区尾的高度
        //    self.headerReferenceSize = CGSizeMake(0, 50);
        //    self.footerReferenceSize = CGSizeMake(0, 50);
    }
    return self;
}
@end

MyCell.h

@interface MyCell : UICollectionViewCell
@property(nonatomic,strong)UIImageView *backgroundImageView;
@property(nonatomic,strong)UIImageView *imageView;
@property(nonatomic,strong)UILabel *label;
@end

MyCell.m

@implementation MyCell
-(instancetype)initWithFrame:(CGRect)frame{
    if(self = [super initWithFrame:frame]){
        self.imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height - 20)];
        [self.contentView addSubview:self.imageView];
        self.label = [[UILabel alloc]initWithFrame:CGRectMake(0, frame.size.height -20, frame.size.width, 20)];
        self.label.backgroundColor = [UIColor blueColor];
        self.label.textAlignment = NSTextAlignmentCenter;
        self.label.font = [UIFont systemFontOfSize:22];
        self.label.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:self.label];
        self.backgroundImageView =[[UIImageView alloc]init];
        self.backgroundView = self.backgroundImageView;
    }
    return self;
}
{
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.window.rootViewController = [[UINavigationController alloc]initWithRootViewController:[[ViewController alloc]init]];
    [self.window makeKeyAndVisible];
    return YES;
}

ViewController.m

#import "ViewController.h"
#import "MyLayout.h"
#define MAS_SHORTHAND_GLOBALS
#import "Masonry.h"
#import "MyCell.h"
static NSString *const cellId = @"cell";
@interface ViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property(nonatomic,strong)UICollectionView*cv;
@end

@implementation ViewController
-(UICollectionView *)cv{
    if(!_cv){
        _cv = [[UICollectionView alloc]initWithFrame:CGRectNull collectionViewLayout:[[MyLayout alloc]init]];
        _cv.dataSource = self;
        _cv.delegate = self;
        //注册cell
        [_cv registerClass:[MyCell class] forCellWithReuseIdentifier:cellId];
        [self.view addSubview:_cv];
        [_cv mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(0);
            
        }];
    }
    return _cv;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self cv];
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 30;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    MyCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];
    cell.imageView.image = [UIImage imageNamed:@"timg"];
    cell.label.text = @"这是单元格";
    return cell;
}

MyCell.h

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface MyCell : UICollectionViewCell
@property(nonatomic,strong)UIImageView *imageView;
@property(nonatomic,strong)UILabel *label;
@end

NS_ASSUME_NONNULL_END

MyCell.m

#import "MyCell.h"
#define MAS_SHORTHAND_GLOBALS
#import "Masonry.h"
@implementation MyCell
-(instancetype)initWithFrame:(CGRect)frame{
    if(self = [super initWithFrame:frame]){
        [self imageView];
        [self label];
    }
    return self;
}
- (UIImageView *)imageView{
    if(!_imageView){
        _imageView = [[UIImageView alloc]init];
        [self.contentView addSubview:_imageView];
        [_imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.equalTo(0);
            make.height.equalTo(self.imageView.mas_width);
        }];
    }
    return _imageView;
}
-(UILabel *)label{
    if(!_label){
        _label = [[UILabel alloc]init];
        _label.backgroundColor = [UIColor yellowColor];
        _label.textAlignment = NSTextAlignmentCenter;
        _label.font = [UIFont systemFontOfSize:14];
        [self.contentView addSubview:_label];
        [_label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(0);
            make.top.equalTo(self.imageView.mas_bottom);
        }];
    }
    return _label;
}

MyLayout.m

#import "MyLayout.h"
@implementation MyLayout
-(instancetype)init{
    if (self = [super init] ) {
        CGSize screenSize = [UIScreen mainScreen].bounds.size;
        CGFloat itemWidth = (screenSize.width - 20 *2 -20*2) / 3;
        //设置每项大小
        self.itemSize = CGSizeMake(itemWidth, itemWidth+20);
        //设置最小行距
        self.minimumLineSpacing = 5;
        //设置最小项间距
        self.minimumInteritemSpacing = 20;
        //设置 分区中的内边距
        self.sectionInset = UIEdgeInsetsMake(5, 20, 5, 20);
        //设置滚动方向
        self.scrollDirection = UICollectionViewScrollDirectionVertical;
        //设置分区头和分区尾的高度
        //    self.headerReferenceSize = CGSizeMake(0, 50);
        //    self.footerReferenceSize = CGSizeMake(0, 50);
    }
    return self;
}
上一篇下一篇

猜你喜欢

热点阅读