美文网首页
关于collectionView的重复利用

关于collectionView的重复利用

作者: hunterzhu | 来源:发表于2016-07-17 09:52 被阅读132次

    在我们平时的的APP里面的图片展示就是在一个UIScrollView中创建一个ImageView,比较简单的做法就是将有多少张图片就建多少个ImageView,这种做法大大的给内存增加了负担。更可能用户在看了前面的一两张图片后,就关闭了,后面创建好的五,六张图片用户就没有看到,资源的利用率就缩水了。

    比较优化的方法:就是创建三个UIImageView,然后改变其图片的内容,就是上节代码所说的那样。

    今天我们将更加优化的方法,利用collectionView来创建图片展示

    1.在storyboard上创建一个UICollectionView
    2.更改宽高
    3.创建一个UICollectionViewCell的类

    .h文件:

    #import <UIKit/UIKit.h>
    
    @interface CollectionCell : UICollectionViewCell
    
    @property(nonatomic,strong)NSString *icon;
    
    @end
    

    .m文件

    
    #import "CollectionCell.h"
    
    @interface CollectionCell ()
    
    @property(nonatomic,strong)UIImageView *imageView;
        
        
    @end
    
    
    @implementation CollectionCell
    
    - (instancetype)initWithFrame:(CGRect)frame {
        
        self = [super initWithFrame:frame];
        if (self != nil) {
            
            //CollectonViewCell的大小
            self.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 200);
            
            //设置Collection上的imageView
            UIImageView *imageView = [[UIImageView alloc] init];
    
            //添加到imageView上
            [self addSubview:imageView];
            
            //赋值
            self.imageView = imageView;
        }
        return self;
    }
    
    - (void) setIcon:(NSString *)icon {
        
        _icon = [icon copy];
        //获取数据
        self.imageView.image = [UIImage imageNamed:icon];
        
    }
    - (void)layoutSubviews {
        
        [super layoutSubviews];
        //固定imageView的位置大小
        self.imageView.frame = self.bounds;      
    }
    @end
    
    

    4.连接ViewContoller与storyboard中的UIContentView空间

    .m文件

    
    #import "ViewController.h"
    
    #import "CollectionCell.h"
    
    //定义ID
    #define ID @"cell"
    
    @interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource> {
        
        //获取数据源
        NSArray *arrImage;
        
    }
    //插座
    @property(nonatomic,weak)IBOutlet UICollectionView *colectionView;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        
        [super viewDidLoad];
        //数据源
        arrImage = @[@"01",@"02",@"03",@"04",@"05"];
        //注册ID,最大的特点就是只占用了两个内存空间
        [self.colectionView registerClass:[CollectionCell class] forCellWithReuseIdentifier:ID];
        //代理 数据源 方法 ,你懂的
        _colectionView.delegate = self;;
        _colectionView.dataSource = self;
        
    }
    
    #pragma mark - 组数
    - (NSInteger) numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
        
        return 10;
    }
    
    #pragma mark - 个数
    - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
        
        return arrImage.count;
    }
    
    #pragma mark - cell 与tableView相似
    - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
        //注册ID
        CollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
        //获取数据
        cell.icon = arrImage[indexPath.item];
    
        return cell;
    }
    @end
    
    
    storyboard中的控件
    效果图

    你会发现只占用了两个内存地址空间,非常神奇吧,赶紧试试吧!


    占用的内存空间

    相关文章

      网友评论

          本文标题:关于collectionView的重复利用

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