美文网首页
初涉CollectionView

初涉CollectionView

作者: 心底碎片 | 来源:发表于2016-08-24 16:41 被阅读3次

1.初始化一个collectionView

UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumInteritemSpacing = 5;
    layout.minimumLineSpacing = 5;
    //定义每个item的大小
    layout.itemSize = CGSizeMake(120, 150);
    
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) collectionViewLayout:layout];
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    
    //注册cell
    [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];

    [self.view addSubview:_collectionView];

2.实现代理方法

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _dataArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    UIImageView * imageV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    imageV.image = [UIImage imageNamed:_dataArray[indexPath.row]];
    
    
    UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, 100, 21)];
    label.text = [NSString stringWithFormat:@"第%ld张图", indexPath.row];
    label.textAlignment = NSTextAlignmentCenter;
    //一定要先移除之前的,不然会重叠
    for (UIView * view in cell.contentView.subviews) {
        [view removeFromSuperview];
    }
    [cell.contentView addSubview:imageV];
    [cell.contentView addSubview:label];
    return cell;
    
}

相关文章

  • 初涉CollectionView

    1.初始化一个collectionView 2.实现代理方法

  • 初涉.

  • 初涉

    环境造就格局预定义人生, 人们忽略换位想掌控乾坤。 骄傲而固执的人认为世间没有对错, 平静的银河系中无数的星系在黑...

  • 初涉npm

    NPM是什么 NPM(node package manager Node包管理器)以往的Web开发中,JS文件数量...

  • 诗坛初涉

    诗坛初涉 写了多少首, 我从未细数; 改了多少次, 也未在意。 怎知道, 语言是否传统。 怎知道, 又有多少层次...

  • 初涉webpack

    什么是webpack? 本质上,webpack 是一个现代 JavaScript 应用程序的静态模块打包器(mod...

  • 诗坛初涉

    七绝 初涉诗坛 天外孤鸿-詹 初登大雅君休笑,巧艺沽空盼论心。 一字恩深同再造,推敲賈岛始成吟。

  • 工笔初涉

    小田老师的书画课前阵子开始教画画了,我们这帮学生三天打鱼两天晒网的,当然也不着急,慢悠悠的学,慢悠悠的画。尤其是工...

  • 初涉管理

    1.自己的立场非常重要 在复杂的机关当中,每个人都有自己的立场领导有领导的立场。二级单位有二级单位的立场,但是要时...

  • 初涉谈判

    最近的一次谈判,费劲周折才安排好时间,也计划了很多次,预想过各种场景,设定好了初步谈判目的、优选方案、次选方案和最...

网友评论

      本文标题:初涉CollectionView

      本文链接:https://www.haomeiwen.com/subject/vglosttx.html