美文网首页
网格试图

网格试图

作者: b6e7b0be2603 | 来源:发表于2018-12-20 15:45 被阅读0次

    #import "ViewController.h"

    #import "MyCollectionViewCell.h"

    #import "HeaderCollectionReusableView.h"

    #import "FooterCollectionReusableView.h"

    #import "NewViewController.h"

    @interface ViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>{

        NSDictionary *_dic;

      }

    //网格视图的对象

    @property(nonatomic,strong)UICollectionView *myCollection;

    @end

    @implementation ViewController

    //重写get方法

    -(UICollectionView*)myCollection{

        if (!_myCollection) {

            //初始化布局对象

            UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];

            //设置每个模块的大小

            layout.itemSize=CGSizeMake(100, 100);

            //设置滚动方向

    //        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;

            layout.scrollDirection = UICollectionViewScrollDirectionVertical;

            //设置最小行间距

            layout.minimumLineSpacing= 10;

            //设置最小列间距

            layout.minimumInteritemSpacing = 5;

            //设置头视图的大小 自动适配  (如果是垂直滚动的话 宽自动是屏幕的宽,可以设置高度。反之如果是水平滚动的话 高自动是屏幕的高,可以设置宽度。)

            layout.headerReferenceSize = CGSizeMake(100, 30);

            //设置尾视图的大小

            layout.footerReferenceSize = CGSizeMake(100, 30);

            //设置每个模块中 上 左 下 右的间距(模块的边界和模块内容的距离)

            layout.sectionInset=UIEdgeInsetsMake(10, 10, 10, 10);

    //        _myCollection  = [[UICollectionView alloc]initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:layout];

            _myCollection  = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:layout];

            //设置代理

            _myCollection.delegate = self;

            _myCollection.dataSource = self;

            //注册单元格

            [_myCollection registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:@"collectionCellId"];

            //注册头视图单元格

            [_myCollection registerClass:[HeaderCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];

            //注册尾视图单元格

            [_myCollection registerClass:[FooterCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer"];

            //设置背景颜色

            _myCollection.backgroundColor = [UIColor lightGrayColor];

        }

        return _myCollection;

    }

    - (void)viewDidLoad {

        [super viewDidLoad];

        // Do any additional setup after loading the view, typically from a nib.

        [self.view addSubview:self.myCollection];

        //模拟数据

       // _dic =@{@"野兽":@[@"小滨",@"小李",@"小蒋",@"小泽"],

    //            @"帅哥":@[@"小雨",@"小惠",@"小建",@"小金",@"小尹",@"小石头"],

    //            @"美女":@[@"小白",@"小玉",@"小霜",@"小小白"],

    //            @"异类":@[@"小黑",@"陈硕",@"光头"]

    //            };

    _dic = @{@"野兽":@[@{@"小滨":@"1.jpg"},@{@"小李":@"2.jpg"},@{@"小蒋":@"5.jpg"},@{@"小泽":@"3.jpg"}],

             @"帅哥":@[@{@"小雨":@"1.jpg"},@{@"小惠":@"3.jpg"},@{@"小建":@"1.jpg"},@{@"小金":@"4.jpg"},@{@"小尹":@"6.jpg"},@{@"小石头":@"1.jpg"}],

             @"美女":@[@{@"小白":@"3.jpg"},@{@"小玉":@"7.jpg"},@{@"小霜":@"4.jpg"},@{@"小小白":@"2.jpg"}],

             @"异类":@[@{@"小黑":@"5.jpg"},@{@"陈硕":@"7.jpg"},@{@"光头":@"6.jpg"}]

                 };

    }

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

        return _dic.count;

    }

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

        NSString*str =_dic.allKeys[section];

        return [[_dic objectForKey:str] count];

    }

    // The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:

    - (UICollectionViewCell*)collectionView:(UICollectionView*)collectionView cellForItemAtIndexPath:(NSIndexPath*)indexPath{

        static NSString *cellId =@"collectionCellId";

        MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellId  forIndexPath:indexPath];

        NSString *str = _dic.allKeys[indexPath.section];

        NSString *name = [[[_dic objectForKey:str]objectAtIndex:indexPath.row] allKeys][0];

        cell.imageView.image = [UIImage imageNamed:[[_dic objectForKey:str] objectAtIndex:indexPath.row][name]];

        cell.label.text=name;

        cell.backgroundColor = [UIColor redColor];

        returncell;

    }

    //设置头视图 和尾视图

    -(UICollectionReusableView*)collectionView:(UICollectionView*)collectionView viewForSupplementaryElementOfKind:(NSString*)kind atIndexPath:(NSIndexPath*)indexPath{

        if([kind isEqualToString:UICollectionElementKindSectionHeader]){

           staticNSString*cellId =@"header";

            HeaderCollectionReusableView *headerCell = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:cellId forIndexPath:indexPath];

            headerCell.label.text= [NSStringstringWithFormat:@"第%ld组",indexPath.section+1];

            headerCell.backgroundColor = [UIColor yellowColor];

            returnheaderCell;

        }else{

            staticNSString*cellId =@"footer";

            FooterCollectionReusableView*footerCell  = [collectionViewdequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:cellId forIndexPath:indexPath];

            footerCell.label.text=@"结尾";

            footerCell.backgroundColor = [UIColor greenColor];

            returnfooterCell;

        }

    }

    -(void)collectionView:(UICollectionView*)collectionView didSelectItemAtIndexPath:(NSIndexPath*)indexPath{

        NSLog(@"第%ld分区---第%ld行",indexPath.section,indexPath.row);

        NewViewController  *new = [[NewViewController alloc]init];

        NSString *str = _dic.allKeys[indexPath.section];

        NSString *name = [[[_dic objectForKey:str]objectAtIndex:indexPath.row] allKeys][0];

        new.name= name;

        new.img = [UIImage imageNamed:[[_dic objectForKey:str] objectAtIndex:indexPath.row][name]];

        [self presentViewController:new animated:YES completion:nil];

    }

    - (void)didReceiveMemoryWarning {

        [super didReceiveMemoryWarning];

        // Dispose of any resources that can be recreated.

    }

    @end

    #import <UIKit/UIKit.h> @interface NewViewController : UIViewController @property(nonatomic,strong)UIImage *img; @property(nonatomic,strong)NSString *name; @end

    #import "NewViewController.h"

    @interface NewViewController ()

    @property(nonatomic,strong)UIImageView *imageView;

    @property(nonatomic,strong)UILabel *label;

    @end

    @implementationNewViewController

    -(UIImageView*)imageView{

        if (!_imageView) {

            _imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-50)];

        }

        return _imageView;

    }

    -(UILabel*)label{

        if(!_label) {

            _label = [[UILabel alloc]initWithFrame:CGRectMake(200, self.view.frame.size.height-40, 200, 30)];

        }

        return _label;

    }

    - (void)viewDidLoad {

        [super viewDidLoad];

        self.view.backgroundColor = [UIColor whiteColor];

        [self.view addSubview:self.imageView];

        [self.view addSubview:self.label];

        self.imageView.image = self.img;

        self.label.text = self.name;

    }

    -(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event{

        [self dismissViewControllerAnimated:YES completion:nil];

    }

    - (void)didReceiveMemoryWarning {

        [super didReceiveMemoryWarning];

        // Dispose of any resources that can be recreated.

    }

    #import

    @interfaceMyCollectionViewCell :UICollectionViewCell

    @property(nonatomic,strong)UIImageView *imageView;

    @property(nonatomic,strong)UILabel *label;

    @end

    #import "MyCollectionViewCell.h"

    @implementationMyCollectionViewCell

    -(UIImageView*)imageView{

        if (!_imageView) {

            _imageView = [[UIImageView alloc]initWithFrame:CGRectMake(20, 10, 60, 50)];

        }

        return _imageView;

    }

    -(UILabel*)label{

        if(!_label) {

            _label = [[UILabel alloc]initWithFrame:CGRectMake(30, 70, 80, 20)];

        }

        return _label;

    }

    -(instancetype)initWithFrame:(CGRect)frame{

        if(self= [superinitWithFrame:frame]) {

            [self addSubview:self.imageView];

            [self addSubview:self.label];

        }

        return self;

    }

    @end

    相关文章

      网友评论

          本文标题:网格试图

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