//
// 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
网友评论