• 01-08 UICollectionView 例子


    1. #import "TimeCollectionCell.h"  

    2. #import <UIKit/UIKit.h>   

      typedef void (^ReturnBlock)(NSString *showText);

      @interface TimeCollectionCell : UICollectionViewCell

      @property (nonatomic,strong)UILabel *timeLab;

      @property(nonatomic,strong) UIButton *btn;

      @property (nonatomic, copy) ReturnBlock returnTextBlock;

      - (void)returnText:(ReturnBlock)block;

      @end

    3. #import "TimeCollectionCell.m"
    4. #import "TimeCollectionCell.h"

       

      @implementation TimeCollectionCell

      - (instancetype)initWithFrame:(CGRect)frame{

          self = [super initWithFrame:frame];

          if (self) {

              

      //        self.timeLab = [[UILabel alloc] initWithFrame:self.contentView.bounds];

      //        self.timeLab = [[UILabel alloc] initWithFrame:CGRectMake(5, 5,40 , 40)];

      //        

      ////        _timeLab.backgroundColor = [UIColor lightGrayColor];

      //        _timeLab.backgroundColor=[UIColor redColor];

      //        _timeLab.font =[UIFont systemFontOfSize:15];

      //        _timeLab.textAlignment = NSTextAlignmentCenter;

      //        [self.contentView addSubview:self.timeLab];

              

              _btn=[[UIButton alloc]initWithFrame:CGRectMake(5, 5, 40 , 40)];

              _btn.backgroundColor=[UIColor redColor];

              [_btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];

              [self.contentView addSubview:_btn];

              

          }

          return self;

      }

       

       

      -(void)btnClick:(UIButton *)btn{

       

          NSLog(@"hh");

          if (self.returnTextBlock != nil) {

              self.returnTextBlock(@"你好!");

          }

       

      }

       

      -(void)returnText:(ReturnBlock)block{

          

          self.returnTextBlock = block;

          

      }

       

       

      #pragma mark  lizi

      /*

       

       #import "YYimageCell.h"

       

       @interface YYimageCell ()

       @property(nonatomic,strong)UIImageView *imageView;

       @end

       @implementation YYimageCell

       

       - (id)initWithFrame:(CGRect)frame

       {

       self = [super initWithFrame:frame];

       if (self) {

       

       UIImageView *imageView=[[UIImageView alloc]init];

       [self addSubview:imageView];

       self.imageView=imageView;

       }

       return self;

       }

       

       -(void)setIcon:(NSString *)icon

       {

       _icon=[icon copy];

       self.imageView.image=[UIImage imageNamed:icon];

       }

       

       

       -(void)layoutSubviews

       {

       [super layoutSubviews];

       self.imageView.frame=self.bounds;

       }

       */

       

      @end

    5. #import "AppDelegate.h"

      #import "GzCollectionViewController.h"

      @interface AppDelegate ()

       

      @end

       

      @implementation AppDelegate

       

       

      - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

          _window=[[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];

          GzCollectionViewController  *cc=[GzCollectionViewController new];

          UINavigationController *nav=[[UINavigationController alloc]initWithRootViewController:cc];

          _window.rootViewController=nav;

          

          [_window makeKeyAndVisible];

          return YES;

      }

    6.  

      主函数

    7. #import "GzCollectionViewController.h"

      #import "TimeCollectionCell.h"

      @interface GzCollectionViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>{

          UICollectionView *_collectionView;

      }

       

      @end

       

      #define SCREENW [UIScreen mainScreen].bounds.size.width

      #define SCREENH [UIScreen mainScreen].bounds.size.height

       

      @implementation GzCollectionViewController

       

      - (void)viewDidLoad {

          [super viewDidLoad];

          self.view.backgroundColor=[UIColor yellowColor];

          

          self.title=@"瀑布流";

          

      #pragma mark  http://www.cnblogs.com/wendingding/p/3888797.html  文顶顶    http://www.cnblogs.com/wendingding/p/3888642.html   http://www.cnblogs.com/wendingding/p/3890953.html

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

      //    [flowLayOut setItemSize:CGSizeMake((SCREENW - 20)/6, 30)];

      //    [flowLayOut setItemSize:CGSizeMake(200, 100)];

          flowLayOut.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);

          

          

          _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREENW, SCREENH) collectionViewLayout:flowLayOut];

          _collectionView.dataSource = self;

          _collectionView.delegate = self;

          _collectionView.backgroundColor = [UIColor yellowColor];

          [self.view addSubview:_collectionView];

          

          

          [_collectionView registerClass:[TimeCollectionCell class] forCellWithReuseIdentifier:@"cell"];

       

      // 6     [self.collectinView registerNib:[UINib nibWithNibName:@"YYcell" bundle:nil] forCellWithReuseIdentifier:YYIDCell];

      }

       

      -(void)preference{

      /*'''

       

       

       50     //默认处于第0组的第500个模型的左边

       51 //    [self.collectinView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:500 inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];

       52

       53      [self.collectinView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:50] atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];

       

       

       

       

       

        //3)通过动画滚动到下一个位置

       37      [self.collectinView scrollToItemAtIndexPath:nextIndexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];

       

       

       */

       

      }

       

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

          return 2 ;

      }

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

          return 5;

      }

       

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

          static NSString *identifier = @"cell";

          

          //也可以 tableviewcell 那样的在这里面写,但是可能会出错  (可能会出错的点  好像是三点 ???)

          

          TimeCollectionCell *cell =(TimeCollectionCell*) [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];

          cell.layer.borderColor = [UIColor lightGrayColor].CGColor;

          cell.layer.borderWidth = 2;

      //    

      //    cell.timeLab.backgroundColor =[UIColor whiteColor];

          cell.backgroundColor =[UIColor greenColor];

      //    cell.timeLab.textColor=[UIColor redColor];

      //    cell.timeLab.text = @"200";

          

          

          

          [cell.btn  setTitle:@"200" forState:UIControlStateNormal];

           __block  NSString  *reStr;

          [cell returnText:^(NSString *showText) {

                reStr=showText;

       

          }];

         

      //    NSLog(@"....heihei......");

          

          return cell ;

      }

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

       

          NSLog(@"嘿嘿  选中了 !!!");

          

      }

       

      //定义每个UICollectionView 的大小

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

      {

      //    return CGSizeMake((SCREENW - 10)/5, 26);

          return CGSizeMake(60,60);

      }

       

      //- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{

      //    return 0;

      //}

      //- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{

      //    return 0;

      //}

      @end

       

  • 相关阅读:
    Jmeter(七)Jmeter脚本优化(数据与脚本分离)
    Jmeter(六)Jmeter脚本包含要素及书写习惯
    Redis的持久化之AOF方式
    Redis的持久化之RDB方式
    Redis持久化介绍
    Redis Keys的通用操作
    Redis的数据结构之sorted-set
    Redis的数据结构之哈希
    Redis的数据结构之字符串
    Jedis 连接池实例
  • 原文地址:https://www.cnblogs.com/gzz2016/p/5113214.html
Copyright © 2020-2023  润新知