zoukankan      html  css  js  c++  java
  • iOS UI 18 uicollectionview和自定义cell

    //

    //  RootViewController.m

    //  Ui - 19  _ UICollectionView

    //

    //  Created by dllo on 15/12/3.

    //  Copyright (c) 2015 dllo. All rights reserved.

    //


    #import "RootViewController.h"

    #import "CustomCollectionViewCell.h"

    @interface RootViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>


    @end


    @implementation RootViewController


    - (void)viewDidLoad {

        [super viewDidLoad];

        self.view.backgroundColor = [UIColor whiteColor];

        

        //uicollectviewLayout 是一个抽象类,我们一般使用他的子类UICollectionViewFlowLayout

        UICollectionViewFlowLayout *flowl = [[[UICollectionViewFlowLayout alloc]init]autorelease];

        //行间距

        //注意:1, 系统会尽可能靠近此参数,并保证不会小于

        //垂直与滑动方向即为行!

        flowl.minimumLineSpacing = 50;

      

        //列间距

        flowl.minimumInteritemSpacing = 50;

        //单元大小

        flowl.itemSize = CGSizeMake(100, 100);

        //区头间据

        flowl.headerReferenceSize = CGSizeMake(50, 50);

        

        //区脚间距

        flowl.footerReferenceSize = CGSizeMake(100, 100);

        

        

        //与屏幕四周的间距

        flowl.sectionInset = UIEdgeInsetsMake(10, 30, 10, 30);

        //滚动方向

        flowl.scrollDirection = UICollectionViewScrollDirectionHorizontal;

        

        

        UICollectionView *collectionV = [[UICollectionView alloc]initWithFrame:self.view.frame collectionViewLayout:flowl];

        collectionV.backgroundColor = [UIColor purpleColor];

        collectionV.delegate = self;

        collectionV.dataSource = self;

        [self.view addSubview:collectionV];

        [collectionV release];

        

        //注册自定义cell

        [collectionV registerClass:[CustomCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];

        

        

        

        // Do any additional setup after loading the view.

    }

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

    {

        return 100;

    }

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

    {

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

        cell.label1.text =[NSString stringWithFormat:@"%ld",indexPath.row];

        return cell;

        

    }

    - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView

    {

        return 3;

    }

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

    {

        NSLog(@"点击%ld", indexPath.row);

    }

    - (void)didReceiveMemoryWarning {

        [super didReceiveMemoryWarning];

        // Dispose of any resources that can be recreated.

    }


    /*

    #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.

    }

    */


    @end

    //

    //  CustomCollectionViewCell.h

    //  Ui - 19  _ UICollectionView

    //

    //  Created by dllo on 15/12/3.

    //  Copyright (c) 2015 dllo. All rights reserved.

    //


    #import <UIKit/UIKit.h>


    @interface CustomCollectionViewCell : UICollectionViewCell

    @property (nonatomic, retain)UILabel *label1;

    @end


    //

    //  CustomCollectionViewCell.m

    //  Ui - 19  _ UICollectionView

    //

    //  Created by dllo on 15/12/3.

    //  Copyright (c) 2015 dllo. All rights reserved.

    //


    #import "CustomCollectionViewCell.h"


    @implementation CustomCollectionViewCell

    - (void)dealloc

    {

        [_label1 release];

        [super dealloc];

        

    }

    - (instancetype)initWithFrame:(CGRect)frame

    {

        self = [super initWithFrame:frame];

        if (self) {

            [self create];

        }

        return self;

    }

    - (void)create

    {

        self.backgroundColor = [UIColor redColor];

        

        

        self.label1 = [[UILabel alloc]initWithFrame:CGRectMake(0,0 , 30, 30)];

        [self.contentView addSubview:self.label1];

        self.label1.backgroundColor = [UIColor whiteColor];

        [self.label1 release];

    }

    @end



  • 相关阅读:
    Appium+python自动化27-等待activity出现(android特有的wait_activity)
    Appium+python自动化26-模拟手势点击坐标(tap)
    Appium+python自动化25-windows版appium_desktop_V1.7.1
    Appium+python自动化24-滑动方法封装(swipe)
    Appium+python自动化23-Appium Desktop
    Appium+python自动化22-DesiredCapabilities详解
    Appium+python自动化20-iOS模拟器(iOS Simulator)安装自家APP
    Appium+python自动化21-查看iOS上app元素属性
    pip3安装pymsql出现的No matching distribution found for pymysql 解决办法
    树莓派升级Python至3.6
  • 原文地址:https://www.cnblogs.com/yuhaojishuboke/p/5043069.html
Copyright © 2011-2022 走看看