IOS学习之 -- UICollectionView

1、实现代理

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@end

2、声明UICollectionView

@property (nonatomic, strong) UICollectionView *myCollectionView;

3、实现代理方法

/**
  *数据源代理
  */
//返回个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    return 50;
}

//返回单元格,自定义单元格很重要的部分
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cid forIndexPath:indexPath];
    cell.backgroundColor = [UIColor orangeColor];
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 80, 80)];
    imageView.image = [UIImage imageNamed:@"image.jpg"];
    [cell.contentView addSubview:imageView];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(100, 35, 100, 40)];
    label.text = @"路飞";
    label.textColor = [UIColor whiteColor];
    [cell.contentView addSubview:label];

    return cell;
}

 /**
  *布局代理
  */
//设置边距
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{

    return UIEdgeInsetsMake(0, 0, 0, 0);
}

//设置item宽高
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{

    return CGSizeMake(self.myCollectionView.bounds.size.width, 80);
}

//选择item
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
    cell.backgroundColor = [UIColor redColor];
    NSLog(@"选中了---> %lu", indexPath.row);
}

//点击后的item
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
    cell.backgroundColor = [UIColor orangeColor];
}

4、绑定View

- (void)viewDidLoad {
    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.
    //布局
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    //创建CollectionView
    self.myCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(20, 30, self.view.bounds.size.width-40, self.view.bounds.size.height-60) collectionViewLayout:flowLayout];

    //注册Cell单元
    [self.myCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:cid];

    self.myCollectionView.dataSource = self;
    self.myCollectionView.delegate = self;
    self.myCollectionView.backgroundColor = [UIColor whiteColor];

    [self.view addSubview:self.myCollectionView];
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,217评论 25 708
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,908评论 18 139
  • 1. 官网下载 Mysql 版本,解压到D盘。 2. 安装目录下有个文件夹:my-default.ini ,重命名...
    盖文哥阅读 247评论 0 0
  • 惟木从绳则正,后从谏则圣。 《尚书·说命上》 句意:木依从绳墨砍削就会正直,君主依从谏言行事就会圣明。 后:君王。
    xcy无名阅读 2,610评论 0 0
  • 我,住在上海,但我并不是上海人。 当初选择在这个城市定居,有很大一部分原因是因为这个城市的建筑。我很喜欢这些精致并...
    弈词阅读 260评论 0 1