UICollectionView

  1. //
  2. //  CollectionController.h
  3. //  MGuardian
  4. //
  5. //  Created by krmao on 16/5/13.
  6. //  Copyright © 2016年 krmao. All rights reserved.
  7. //
  8. #import <UIKit/UIKit.h>
  9. @interface CollectionController : UIViewController
  10. @end
  11. //********************************************************************************
  12. //*自定义类 UICollectionViewCell START
  13. //********************************************************************************
  14. //静态变量声明定义
  15. static NSString * __nonnull collectionCellID=@"mmcell";//可复用的reuseIdentifier
  16. @interface MCollectionItemCellView : UICollectionViewCell
  17. @property (weak, nonatomic) IBOutlet UILabel *mLabelView;
  18. + (MCollectionItemCellView * __nonnull)getCellView: (UICollectionView * __nonnull) uiCollectionView  index:(NSIndexPath * __nonnull)indexPath;
  19. @end
  20. //********************************************************************************
  21. //*自定义类 UICollectionViewCell 实现
  22. //********************************************************************************
  23. @implementation MCollectionItemCellView
  24. - (id __nonnull)initWithFrame:(CGRect)frame{
  25.    self=[super initWithFrame:frame ];
  26.    return self;
  27. }
  28. //静态工厂方法
  29. + (MCollectionItemCellView * __nonnull)getCellView: (UICollectionView * __nonnull) uiCollectionView  index:(NSIndexPath * __nonnull)indexPath;{
  30.    MCollectionItemCellView * cellView =(MCollectionItemCellView *) [uiCollectionView dequeueReusableCellWithReuseIdentifier:collectionCellID forIndexPath:indexPath];
  31.    //设置cell选中状态
  32.    //cellView.selectionStyle=UITableViewCellSelectionStyleDefault;
  33.    cellView.selectedBackgroundView=[[UIView alloc]initWithFrame:cellView.frame];
  34.    cellView.selectedBackgroundView.backgroundColor=[UIColor greenColor];
  35.    [cellView sizeToFit];
  36.    return cellView;
  37. }
  38. @end
  39. //********************************************************************************
  40. //*自定义类 UICollectionViewCell END
  41. //********************************************************************************
  1. //
  2. //  CollectionController.m
  3. //  MGuardian
  4. //
  5. //  Created by krmao on 16/5/13.
  6. //  Copyright © 2016年 krmao. All rights reserved.
  7. //
  8. #import "CollectionController.h"
  9. @interface CollectionController ()<UICollectionViewDataSource,UICollectionViewDelegate>
  10. @property (weak, nonatomic) IBOutlet UICollectionView *mCollectionView;
  11. @property NSMutableArray *dataList;
  12. @end
  13. @implementation CollectionController
  14. - (void)viewDidLoad {
  15.    [super viewDidLoad];
  16.    self.mCollectionView.dataSource=self;
  17.    self.mCollectionView.delegate=self;
  18.    self.dataList=[[NSMutableArray alloc]initWithObjects:   @"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",@"i",@"j",@"k",@"l",@"m",
  19.                                                            @"n",@"o",@"p",@"q",@"r",@"s",@"t",@"u",@"v",@"w",@"x",@"y",@"z", nil];
  20. }
  21. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
  22.    return self.dataList.count;
  23. }
  24. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
  25.    MCollectionItemCellView *celView=[MCollectionItemCellView getCellView:collectionView index:indexPath];
  26.    celView.mLabelView.text=[NSString stringWithFormat: @"item:%i-%@",indexPath.row,[self.dataList objectAtIndex:indexPath.row]];
  27.    return celView;
  28. }
  29. @end









来自为知笔记(Wiz)


posted on 2016-05-13 18:46  大魔术师  阅读(141)  评论(0编辑  收藏  举报

导航