热更新demo

leakCollectionController.m 3.6KB

    // // leakCollectionController.m // ThePaperBase // // Created by Huixin on 15/8/3. // Copyright (c) 2015年 scar1900. All rights reserved. // #import "leakCollectionController.h" #import "leakCollectionViewCell.h" static NSString* cellId = @"LeakCollectionViewIdentifier"; @interface leakCollectionController()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UIScrollViewDelegate, leakCollectionViewCellDelegate>{ NSMutableArray *imageList; } @property(nonatomic, strong)UICollectionView *collectView; @end @implementation leakCollectionController - (void)viewDidLoad { [super viewDidLoad]; imageList = NSMutableArray.new; } - (void)viewWillLayoutSubviews { [super viewWillLayoutSubviews]; self.view.backgroundColor = [UIColor clearColor]; self.view.clipsToBounds = NO; if (!_collectView) { [self.view addSubview:self.collectView]; [self.collectView makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(self.view); }]; } } - (UICollectionView*)collectView { if (!_collectView) { CGSize itemSize = CGSizeMake(self.view.frame.size.height, self.view.frame.size.height); CGFloat space = self.view.frame.size.width/4-itemSize.width; UICollectionViewFlowLayout *layout= [[UICollectionViewFlowLayout alloc]init]; layout.minimumInteritemSpacing = space-1; //bug5302: 4s报料,图片显示间距问题 layout.itemSize = itemSize; layout.sectionInset = UIEdgeInsetsMake(0, space, 0, 0); _collectView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout]; [_collectView registerClass:[leakCollectionViewCell class] forCellWithReuseIdentifier:cellId]; _collectView.delegate = self; _collectView.dataSource = self; _collectView.allowsSelection = NO; _collectView.scrollEnabled = NO; _collectView.clipsToBounds = NO; _collectView.backgroundColor = [UIColor clearColor]; _collectView.delaysContentTouches = NO; } return _collectView; } - (NSMutableArray*)getImageList { return imageList; } - (NSInteger)getNumOfImage { if (imageList) { return [imageList count]; } return 0; } - (void)addImage:(UIImage *)image { if (imageList) { [imageList addObject:image]; [self.collectView reloadData]; } } - (void)deleteImage:(leakCollectionViewCell*)cell { if (imageList) { NSIndexPath *indexPath = [_collectView indexPathForCell:cell]; [imageList removeObjectAtIndex:indexPath.row]; [_collectView deleteItemsAtIndexPaths:@[indexPath]]; } if (imageList.count == 0) { if ([self.delegate respondsToSelector:@selector(imageAllDeleted)]) { [self.delegate imageAllDeleted]; } } } #pragma mark - collctionView delegate and datasource - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView*)collectionView { return 1; } - (NSInteger)collectionView:(UICollectionView*)collectionView numberOfItemsInSection:(NSInteger)section { if (imageList) { return imageList.count; }else return 0; } - (UICollectionViewCell*)collectionView:(UICollectionView*)collectionView cellForItemAtIndexPath:(NSIndexPath*)indexPath { leakCollectionViewCell *cell = (leakCollectionViewCell*)[collectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath]; if (imageList && imageList.count>0) { UIImage *img = imageList[indexPath.row]; [cell setImage:img]; cell.delegate = self; cell.hidden = NO; } return cell; } @end