collectionView 复用和注册

//
// ViewController.m
// test-alert
//
// Created by 姜维克 on 2017/4/20.
// Copyright © 2017年 O2O_iOS_jiangweike. All rights reserved.
//

import "ViewController.h"

@interface WKCollectionView : UICollectionView

@end

@interface WKCollectionView ()
//@property (nonatomic, strong) UICollectionView *collection;
@end
@implementation WKCollectionView
@end

@interface WKCollectionViewCell : UICollectionViewCell

@end

@interface WKCollectionViewCell ()
@property (nonatomic, strong) UILabel *testLabel;
@end

@implementation WKCollectionViewCell

//必须重写initWithFrame 方法 否则deque方法 第一次得到的cell 的testLabel是nil

  • (instancetype)initWithFrame:(CGRect)frame
    {
    self = [super initWithFrame:frame];
    if (self) {
    UILabel *label = [[UILabel alloc] initWithFrame:self.contentView.bounds];
    label.text = @"ceshi";
    label.textColor = [UIColor blackColor];
    [self.contentView addSubview:label];

      self.testLabel = label;
      self.contentView.backgroundColor = [UIColor darkGrayColor];
    

    }
    return self;
    }

@end

@interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionView *collection;
@property (nonatomic, strong) NSIndexPath *indexPath;
@property (nonatomic, strong) WKCollectionViewCell *oldCell;
@end

@implementation ViewController

  • (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self test];
    [self initCollectionView];
    }

  • (void)test
    {
    UIAlertView *alert = [[UIAlertView alloc] init];

    UILabel *l = [[UILabel alloc] init];
    l.text = @"下午";
    l.frame = CGRectMake(20, 20, 100, 40);
    [self.view addSubview:l];
    [l setTextColor:[UIColor redColor]];

}

  • (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    }

static NSString * const reuseIdentifier = @"Cell";

  • (void)initCollectionView
    {

    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
    flow.itemSize = CGSizeMake(100, 40);
    flow.scrollDirection = UICollectionViewScrollDirectionVertical;

    WKCollectionView *collection = [[WKCollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flow];
    [collection registerClass:[WKCollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
    collection.dataSource = self;
    collection.delegate = self;
    collection.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:collection];
    self.collection = collection;
    }

pragma mark - UICollectionViewDataSource

  • (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
    }

  • (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 4000;
    }

  • (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    WKCollectionViewCell *cell = (WKCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    //如果重写cell的init方法;dequeueReusableCellWithReuseIdentifier方法 得到的cell 一定不为nil 但是cell的testLabel一定是nil;不会走cell的init方法,而是initWithFrame方法
    if([indexPath isEqual:self.indexPath]){
    cell.testLabel.textColor = [UIColor yellowColor];
    }else{
    cell.testLabel.textColor = [UIColor blackColor];
    }
    //if(!cell) 判断没有效果,因为前面collectionView注册了[WKCollectionViewCell class]
    if(!cell){
    cell = [[WKCollectionViewCell alloc] init];
    }

    return cell;
    }

  • (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (self.indexPath) {
    WKCollectionViewCell *oldCell = (WKCollectionViewCell *)[collectionView cellForItemAtIndexPath:self.indexPath];
    oldCell.testLabel.textColor = [UIColor blackColor];
    // NSLog(@"indexpath :%@",[collectionView indexPathForCell:self.oldCell]);
    }
    //oldCell 记不住 因为每次复用之后 cell的地址就会改变

    WKCollectionViewCell *cell = (WKCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    cell.testLabel.textColor = [UIColor yellowColor];
    // self.oldCell = cell;
    // NSLog(@"点击oldCell:%@",self.oldCell);
    self.indexPath = indexPath;
    }
    @end

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容