热更新demo

channelTopicCateCell.m 5.2KB

    // // channelTopicCateCell.m // ThePaperBase // // Created by YoungLee on 15/8/5. // Copyright (c) 2015年 scar1900. All rights reserved. // #import "channelTopicCateCell.h" #import "cateCell.h" @interface channelTopicCateCell()<UICollectionViewDataSource,UICollectionViewDelegate> @property(nonatomic, strong)UICollectionView *collectView; @end @implementation channelTopicCateCell @synthesize cateList = _cateList; static NSString *cateCellID = @"cateCell"; - (void)awakeFromNib { // Initialization code } - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; // Configure the view for the selected state } -(void)setCateList:(NSMutableArray *)list{ _cateList = list; long columNum; if(_cateList.count % 4 != 0){ columNum = _cateList.count/4 +1; }else{ columNum = _cateList.count/4; } float width = (rect_screen.size.width -20 -30)/4; _collectView.frame =CGRectMake(10, 10, rect_screen.size.width-20, columNum*width*90/150+15); } -(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{ self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { self.backgroundColor = [UIColor colorWithHexString:CELLBACKCOLOR]; [self.contentView addSubview:self.collectView]; [self.collectView registerClass:[cateCell class] forCellWithReuseIdentifier:cateCellID]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(needrefreshNightMode:) name:REFRESHAFTERNIGHTMODE object:nil]; } return self; } - (void)needrefreshNightMode:(id)sender{ self.backgroundColor = [UIColor colorWithHexString:CELLBACKCOLOR]; } -(UICollectionView *)collectView{ if (!_collectView) { // float scale = 90/150; float width = (rect_screen.size.width -20 -30)/4; CGSize itemSize = CGSizeMake(width, width*90/150); CGFloat space = (rect_screen.size.width-width*4-20)/3; UICollectionViewFlowLayout *layout= [[UICollectionViewFlowLayout alloc]init]; layout.scrollDirection = UICollectionViewScrollDirectionVertical; layout.minimumLineSpacing = space; layout.minimumInteritemSpacing = 1; layout.itemSize = itemSize; _collectView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 10, rect_screen.size.width-20, 0) collectionViewLayout:layout]; _collectView.delegate = self; _collectView.dataSource = self; _collectView.clipsToBounds = YES; _collectView.bounces = YES; _collectView.backgroundColor = [UIColor clearColor]; _collectView.allowsSelection = YES; _collectView.scrollEnabled = YES; _collectView.delaysContentTouches = NO; } return _collectView; } //定义展示的UICollectionViewCell的个数 -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return _cateList.count; } //定义展示的Section的个数 -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 1; } //每个UICollectionView展示的内容 -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { cateCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:cateCellID forIndexPath:indexPath]; cell.cateBo = _cateList[indexPath.row]; // cell.indexPath = indexPath; return cell; } //定义每个UICollectionView 的大小 - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { float width = (rect_screen.size.width -20 - 30)/4; return CGSizeMake(width, width *90/150); } //定义每个UICollectionView 的 margin -(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section { return UIEdgeInsetsMake(0, 0, 0, 0); } //UICollectionView被选中时调用的方法 -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { cateCell * cell = (cateCell *)[collectionView cellForItemAtIndexPath:indexPath]; [collectionView deselectItemAtIndexPath:indexPath animated:YES]; [UIView animateWithDuration:0.1 animations:^{ cell.backgroundColor = [UIColor colorWithHexString:BLUECOLOR]; cell.chnName.textColor = [UIColor whiteColor]; cell.enName.textColor = [UIColor whiteColor]; } completion:^(BOOL finished) { cell.backgroundColor = [UIColor colorWithHexString:BackGroundColor]; cell.chnName.textColor = [UIColor colorWithHexString:TextBlack]; cell.enName.textColor = [UIColor colorWithHexString:TextLightGray]; if ([self.delegate respondsToSelector:@selector(returnCate:index:)]) { [self.delegate returnCate:cell.cateBo index:indexPath.row]; } }]; } //返回这个UICollectionView是否可以被选择 -(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath { return YES; } @end