UICollectionViewController简单操作

来源:互联网 发布:js如何验证手机号码 编辑:程序博客网 时间:2024/06/04 18:46
////  MyCollectionViewCell.h//  AppUI组件学习////  Created by 麦子 on 15/6/24.//  Copyright (c) 2015年 麦子. All rights reserved.//#import <UIKit/UIKit.h>@interface MyCollectionViewCell : UICollectionViewCell@property(nonatomic,strong)UILabel *label;@end
////  MyCollectionViewCell.m//  AppUI组件学习////  Created by 麦子 on 15/6/24.//  Copyright (c) 2015年 麦子. All rights reserved.//#import "MyCollectionViewCell.h"@implementation MyCollectionViewCell@synthesize label;- (instancetype)initWithFrame:(CGRect)frameA{    self = [super initWithFrame:frameA];    if (self) {        self.backgroundColor = [UIColor orangeColor];        label = [[UILabel alloc] init];        label.frame = CGRectMake(25, 35, 50, 30);        label.backgroundColor = [UIColor purpleColor];        [self addSubview:label];    }    return self;    }@end

////  MyCollectionViewController.m//  AppUI组件学习////  Created by 麦子 on 15/6/24.//  Copyright (c) 2015年 麦子. All rights reserved.//#import "MyCollectionViewController.h"#import "MyCollectionViewCell.h"@interface MyCollectionViewController ()@end@implementation MyCollectionViewControllerstatic NSString * const reuseIdentifier = @"Cell";- (void)viewDidLoad {    [super viewDidLoad];    self.view.backgroundColor = [UIColor whiteColor];    self.title = @"集合视图";    // Uncomment the following line to preserve selection between presentations    // self.clearsSelectionOnViewWillAppear = NO;        //  注册这个    [self.collectionView registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];    }- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    }#pragma mark <UICollectionViewDataSource>// 分区- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {     return 1;}// 一个区有多少行- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {     return 10;}- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {    MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];    cell.label.text = [NSString stringWithFormat:@"%ld",indexPath.row];    cell.label.textColor = [UIColor redColor];    cell.label.textAlignment = NSTextAlignmentCenter;            return cell;}#pragma mark <UICollectionViewDelegate>/*// Uncomment this method to specify if the specified item should be highlighted during tracking- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {return YES;}*//*// Uncomment this method to specify if the specified item should be selected- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {    return YES;}*//*// Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {return NO;}- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {return NO;}- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {}*/@end

// 集合视图    // 创建流逝布局    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];    // 单元格大小    flow.itemSize = CGSizeMake(100, 100);    // 滚动的方向    flow.scrollDirection = UICollectionViewScrollDirectionVertical;    // 设置边界    flow.sectionInset = UIEdgeInsetsMake(40, 0, 0, 0);        MyCollectionViewController *collectionDemo = [[MyCollectionViewController alloc] initWithCollectionViewLayout:flow];


0 0