IOS_UI_UICollectionView

和UITableView类似,UICollectionView也有两个代理方法

但是多了layout布局 section和item,初始化一个collectionview必须要给定一个layout布局,并且必须在创建的时候就注册要使用的cell;

代码如下:

#import <UIKit/UIKit.h>


@interface AppDelegate : UIResponder <UIApplicationDelegate>


@property (strong, nonatomic) UIWindow *window;



@end

#import "AppDelegate.h"

#import "MainViewController.h"

@interface AppDelegate ()


@end


@implementation AppDelegate

- (void)dealloc

{

    [_window release];

    [super dealloc];

}


- (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 makeKeyAndVisible];

    [_window release];

    

    /*

     UICollectionView 和tableView一样有两个协议 多了layout布局 cell:1.section 2. item

     

     */

    

    MainViewController *mainVC = [[MainViewController alloc]init];

    self.window.rootViewController = mainVC;

    [mainVC release];

    

    

    

    return YES;

}

#import <UIKit/UIKit.h>


@interface MainViewController : UIViewController


@end


#import "MainViewController.h"

#import "MyCollectionViewCell.h"

@interface MainViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>


@end


@implementation MainViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    //UICollectionView 的使用

    // 1. 初始化一个collectionView必须要给定一个layout布局对象

//    UICollectionViewLayout 是一个抽象类 如果要利用的话 必须创建子类 不能直接使用

    //系统写好的子类 UICollectionViewFlowLayout 是苹果写好的一个布局类,用于标准的瀑布流布局

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

    //每个cell大小

    flowLayout.itemSize = CGSizeMake(100, 180);

    //设置一行的话 就用高度控制 高度和设置的液面高度差不多就行

    flowLayout.sectionInset = UIEdgeInsetsMake(20, 20, 10, 20);

    

    UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:flowLayout];

    //左右滚动

    flowLayout.scrollDirectionUICollectionViewScrollDirectionHorizontal;

    //设置间隔距离 最小行间距 不能低于这个数

    flowLayout.minimumLineSpacing = 1;

    //每一行间距不低于设置的数 每一行中两个cell之间的最小距离

//    flowLayout.minimumInteritemSpacing = 5;

    

    collectionView.dataSource = self;

    collectionView.delegate = self;

    [self.view addSubview:collectionView];

    [collectionView release];

    [flowLayout release];

    //2. collectionview 必须在创建的时候就注册要使用的cell

    [collectionView registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];

    

    

    

}


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

{

    return  100;

}

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

{

    MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];

    //3.

    cell.contentView.backgroundColor = [UIColor redColor];

    return cell;

}

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

{

    NSLog(@"%ld",indexPath.item);

}

#import <UIKit/UIKit.h>


@interface MyCollectionViewCell : UICollectionViewCell

@property (nonatomic,retain)UILabel *myLabel;

@end

#import "MyCollectionViewCell.h"


@implementation MyCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame

{

    self = [super initWithFrame:frame];

    if (self) {

        self.myLabel = [[UILabel alloc]init];

        _myLabel.backgroundColor = [UIColor orangeColor];

        _myLabel.textAlignment = NSTextAlignmentCenter;

        [self.contentView addSubview:_myLabel];

        [_myLabel release];

        //布局最好不要再layout subview里

        //如果换布局就用另一个类

    }

    return  self;

}

- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes

{

    //在这里面给collectionviewcell布局子视图

    _myLabel.frame = CGRectMake(5, 5, layoutAttributes.size.width - 10, layoutAttributes.size.height-10);

    

}

- (void)dealloc

{

    [_myLabel release];

    [super dealloc];

}

//fanye

//可用scrollView的属性



@end



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值