zoukankan      html  css  js  c++  java
  • iOS UICollectionView 的简单显示

    #import <UIKit/UIKit.h>

    @interface ViewController : UIViewController<UICollectionViewDataSource,UICollectionViewDelegate>

    @property (nonatomic ,strong) UICollectionView *collection;

    @property (nonatomic ,strong) NSMutableArray *arrImg;

    @end

    #import "ViewController.h"

    @interface ViewController ()

    @end

    @implementation ViewController

    - (void)viewDidLoad {

        [super viewDidLoad];

        self.arrImg = [NSMutableArray array];

        for (int i = 0; i < 47; i++) {

            [self.arrImg addObject:[NSString stringWithFormat:@"%d.jpg",i]];

        }

        //

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

        self.collection = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];

        //self.collection.backgroundColor = [UIColor redColor];

        self.collection.dataSource =self;

        self.collection.delegate = self;

        [self.view addSubview:self.collection];

        

        [self.collection registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"Collection"];

       

    }

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

        return self.arrImg.count;

    }

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

    //

    //{

    //    

    //    return 1 ;

    //    

    //}

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

        UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Collection" forIndexPath:indexPath];

        cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:self.arrImg[indexPath.row]]];

        

        return cell;

    }

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

    {

        

        UICollectionViewCell * cell = ( UICollectionViewCell *)[collectionView cellForItemAtIndexPath :indexPath];

        

        cell. backgroundColor = [ UIColor colorWithRed :(( arc4random ()% 255 )/ 255.0 ) green :(( arc4random ()% 255 )/ 255.0 ) blue :(( arc4random ()% 255 )/ 255.0 ) alpha : 1.0f ];

    }

    -( BOOL )collectionView:( UICollectionView *)collectionView shouldSelectItemAtIndexPath:( NSIndexPath *)indexPath

    {

        

        return YES ;

        

    }

    - ( CGSize )collectionView:( UICollectionView *)collectionView layout:( UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:( NSIndexPath *)indexPath

    {

        

        return CGSizeMake ( 90 , 90 );

        

    }

    -( UIEdgeInsets )collectionView:( UICollectionView *)collectionView layout:( UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:( NSInteger )section

    {

        

        return UIEdgeInsetsMake ( 10 , 10 , 10 , 10 );

        

    }

    - (void)didReceiveMemoryWarning {

        [super didReceiveMemoryWarning];

        // Dispose of any resources that can be recreated.

    }

    运行结果如下: 

    @end

  • 相关阅读:
    kubernetes部署jenkins(Docker in Docker)及认证
    helm生产环境离线安装
    helm在kubernetes环境中搭建
    GlusterFs卷类型分析及创建、使用(结合kubernetes集群分析)
    glusterfs详解及kubernetes 搭建heketi-glusterfs
    kubernetes搭建Harbor无坑及Harbor仓库同步
    生产环境:ansible自动化部署kubernetes-1.14
    Gluserfs 架构详解【译】官网
    k8s部署高可用Ingress
    《A Survey on Transfer Learning》迁移学习研究综述 翻译
  • 原文地址:https://www.cnblogs.com/liumu/p/5331070.html
Copyright © 2011-2022 走看看