zoukankan      html  css  js  c++  java
  • 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"
    
    @interface RootViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
    {
    }
    @end
     static NSString *identifier = @"cell";
    @implementation RootViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
       //创建布局对象
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        // 设置滚动的方向
        [layout setScrollDirection:UICollectionViewScrollDirectionVertical];
        //行的间隙
        layout.minimumLineSpacing = 20;
        //列的间隙
    //    layout.minimumInteritemSpacing = 10;
        //item的大小
        layout.itemSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, 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:[UICollectionViewCell class] forCellWithReuseIdentifier:identifier];
        [self.view addSubview:collectionView];
    }
    // 告诉系统一共有多少组
    - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
        return 1;
    }
    // 告诉系统第section组有多少行
    - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
        return 15;
    }
    // 告诉系统indexPath的第Section组的item行显示什么内容
    - (UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
       
        UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
        cell.backgroundColor = [UIColor redColor];
        return cell;
    }
    
    - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
        NSLog(@"==%lu",indexPath.row);
    }
    
    @end
  • 相关阅读:
    创建maven项目
    有pom.xml文件但是无法用maven构建问题
    Linux关闭防火墙,开放端口
    Java SSH远程执行Shell命令、shell脚本实现(Ganymed SSH)
    linux系统切换用户
    Looksery Cup 2015 C. The Game Of Parity —— 博弈
    Codeforces Round #105 (Div. 2) E. Porcelain —— DP(背包问题)
    Codeforces Round #198 (Div. 2) E. Iahub and Permutations —— 容斥原理
    Educational Codeforces Round 9 C. The Smallest String Concatenation —— 贪心 + 字符串
    Technocup 2017
  • 原文地址:https://www.cnblogs.com/lantu1989/p/5156826.html
Copyright © 2011-2022 走看看