美文网首页
网格视图

网格视图

作者: Wang99 | 来源:发表于2017-11-23 13:03 被阅读0次

CollcetionViewController.m

#import "CollectionViewController.h"

#import "MyCell.h"
@interface CollectionViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
{
    NSMutableArray *theArray1,*theArray2;
    
}

@property(nonatomic,strong) UICollectionView *theCollect;
@property(nonatomic,strong) UICollectionViewFlowLayout *Flow;
@end



@implementation CollectionViewController

-(UICollectionView *)theCollect{
    if (!_theCollect) {
        //创建网格对象
        _theCollect = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:self.Flow];
        //
        _theCollect.delegate = self;
        _theCollect.dataSource = self;
        
        //是否按页滑动
        _theCollect.pagingEnabled = YES;
        _theCollect.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:_theCollect];
        
        //注册单元格
        [_theCollect registerClass:[MyCell class] forCellWithReuseIdentifier:@"Mycell"];

        
    }
    return _theCollect;
}

-(UICollectionViewFlowLayout *)Flow{
    if (!_Flow) {
        _Flow = [[UICollectionViewFlowLayout alloc]init];
        
        //设定单元格的大小
        _Flow.itemSize = CGSizeMake(80, 100);
        //设置滚动方向
        _Flow.scrollDirection = UICollectionViewScrollDirectionVertical;
        //设置最小行间隔
        _Flow.minimumLineSpacing = 10;
        //设置最小列间距
        _Flow.minimumInteritemSpacing = 10;
        
        //设定分区的间距
        _Flow.sectionInset = UIEdgeInsetsMake(270, 0, 0, 0);
        
    }
    return _Flow;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //创建流布局对象
    theArray1 = [NSMutableArray arrayWithObjects:@""......, nil];
    
    [self.view addSubview:self.theCollect];
    
}
#pragma -UICollectionViewDataSource


-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (section == 0)
    {
        return 12;
    }
   
    return 0;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MyCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Mycell" forIndexPath:indexPath];
    
    //
    cell.Img.image = [UIImage imageNamed:@"123.png"];
    
    
    cell.textLb.text = theArray1[indexPath.row];
    cell.textLb.textAlignment = NSTextAlignmentCenter;
    
    return cell;
}
@end
#import <UIKit/UIKit.h>

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

.m


@implementation MyCell
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        [self addSubview:self.Img];
        [self addSubview:self.textLb];
    }
    return self;
}
- (UIImageView *)Img
{
    if (!_Img)
    {
        _Img = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 80, 80)];
        // _Img.layer.cornerRadius = 80/2;
        _Img.layer.masksToBounds = YES;
    }
    return _Img;
}
- (UILabel *)textLb
{
    if (!_textLb)
    {
        _textLb = [[UILabel alloc]initWithFrame:CGRectMake(0, 80, 80, 20)];
        // _textLb.textAlignment = NSTextAlignmentCenter;
        _textLb.textColor = [UIColor blackColor];
        
    }
    return _textLb;
}
@end

相关文章

网友评论

      本文标题:网格视图

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