• iOS UICollectionView之三(基本用法)


    #import <UIKit/UIKit.h>
    
    @interface AppDelegate : UIResponder <UIApplicationDelegate>
    
    @property (strong, nonatomic) UIWindow *window;
    
    
    @end
    #import "AppDelegate.h"
    #import "RootViewController.h"
    @interface AppDelegate ()
    
    @end
    
    @implementation AppDelegate
    
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        // Override point for customization after application launch.
        self.window.backgroundColor = [UIColor whiteColor];
        
        
        self.window.rootViewController = [[RootViewController alloc] init];
        
        
        [self.window makeKeyAndVisible];
        return YES;
    }
    
    
    
    @end
    #import <UIKit/UIKit.h>
    
    @interface RootViewController : UIViewController
    
    @end
    #import "RootViewController.h"
    #import "ImageViewCell.h"
    #define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
    #define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
    #define line_gap 10
    #define interitem_gap 20
    @interface RootViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
    {
    }
    @end
    
    static NSString *identifier = @"cell";
    static NSString *headerIdentifier = @"header";
    static NSString *flooterIdentifier = @"floot";
    @implementation RootViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
       //创建布局对象
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        // 设置滚动的方向
        [layout setScrollDirection:UICollectionViewScrollDirectionVertical];
        //行的间隙
        layout.minimumLineSpacing = line_gap;
        //列的间隙
        layout.minimumInteritemSpacing = 10;
        //item的大小
        layout.itemSize = CGSizeMake((SCREEN_WIDTH - 2*line_gap)/3, 80);
        //创建collectionView
        UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:[[UIScreen mainScreen] bounds] collectionViewLayout:layout];
        collectionView.backgroundColor = [UIColor greenColor];
        // 设置代理
        collectionView.dataSource = self;
        collectionView.delegate = self;
        //告诉系统将来需要创建什么样的cell(在获取cell之前必须先注册一个cell到系统中)
        [collectionView registerClass:[ImageViewCell class] forCellWithReuseIdentifier:identifier];
        // 注册头视图
        [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerIdentifier];
        // 注册尾视图
        [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:flooterIdentifier];
        [self.view addSubview:collectionView];
    }
    // 告诉系统一共有多少组
    - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
        return 1;
    }
    // 告诉系统第section组有多少行
    - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
        return 30;
    }
    // 告诉系统indexPath的第Section组的item行显示什么内容
    - (UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
       
        ImageViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
        cell.photo.image = [UIImage imageNamed:@"cellPhoto"];
        return cell;
    }
    //返回头headerView的大小
    - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
        CGSize size = CGSizeMake(SCREEN_WIDTH, 100+line_gap);
        return size;
    }
    //返回头flooterView的大小
    - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
    {
        CGSize size = CGSizeMake(SCREEN_WIDTH, 100+line_gap);
        return size;
    }
    // 头和尾部显示的内容
    - (UICollectionReusableView*)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
        UICollectionReusableView *reusableView = nil;
        if (kind == UICollectionElementKindSectionHeader) {
            reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerIdentifier forIndexPath:indexPath];
            UIImageView *headerPhoto = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 100)];
            headerPhoto.image = [UIImage imageNamed:@"headerPhoto"];
            [reusableView addSubview:headerPhoto];
        }else if (kind == UICollectionElementKindSectionFooter){
            reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:flooterIdentifier forIndexPath:indexPath];
            UIImageView *flooterPhoto = [[UIImageView alloc] initWithFrame:CGRectMake(0, line_gap, SCREEN_WIDTH, 100)];
            flooterPhoto.image = [UIImage imageNamed:@"flooterPhoto"];
            [reusableView addSubview:flooterPhoto];
        }
        return reusableView;
    }
    
    
    - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
        NSLog(@"==%lu",indexPath.row);
    }
    
    @end
    #import <UIKit/UIKit.h>
    
    @interface ImageViewCell : UICollectionViewCell
    
    @property (nonatomic, strong) UIImageView *photo;
    
    @end
    #import "ImageViewCell.h"
    
    @implementation ImageViewCell
    
    - (instancetype)initWithFrame:(CGRect)frame{
        if (self = [super initWithFrame:frame]) {
            self.photo = [[UIImageView alloc] init];
            self.photo.frame = self.bounds;
            [self addSubview:self.photo];
        }
        return self;
    }
    
    @end
  • 相关阅读:
    观察者模式的结构
    策略模式
    EJB 配置多个数据源
    EJB3 调用的存储过程
    Android学习笔记_49_Android中自定义属性(attrs.xml,TypedArray的使用)
    Android学习笔记_48_若水新闻客户端源码剖析
    博客样式
    oracle 基础知识(四)常用函数
    oracle 基础知识(三)
    oracle 基础语法(二)
  • 原文地址:https://www.cnblogs.com/lantu1989/p/5157080.html
Copyright © 2020-2023  润新知