我在不使用情节提要的情况下以编程方式创建了一个UICollectionView
,UICollectionViewCell
但是发生了几件事,我不明白为什么。
中存在内存泄漏BKPhotoCell
。
单元格是全屏的,但在滚动时CollectionView
,它们从未重复使用。Init
在每个indexpath.row
.
PrepareForReuse
也从来没有叫过。
谁能帮我理解我做错了什么?
- (void)viewDidLoad
{
[super viewDidLoad];
CGSize screenSize = self.view.bounds.size;
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
[flowLayout setItemSize:CGSizeMake(screenSize.width, screenSize.height)];
[flowLayout setMinimumInteritemSpacing:0.0f];
[flowLayout setMinimumLineSpacing:0.0f];
[flowLayout setSectionInset:UIEdgeInsetsMake(0, 0, 0, 0)];
[flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0,0, screenSize.width, screenSize.height) collectionViewLayout:flowLayout];
[self.collectionView setDelegate:self];
[self.collectionView setDataSource:self];
[self.collectionView registerClass:[BKPhotoCell class] forCellWithReuseIdentifier:@"BKPhotoCell"];
[self.collectionView setPagingEnabled:YES];
[self.collectionView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
[self.view addSubview:self.collectionView];
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
BKPhotoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"BKPhotoCell" forIndexPath:indexPath];
[cell.imageView setImage:[UIImage imageNamed:[_photos objectAtIndex:indexPath.row]]];
return cell;
}
BKPhotoCell.h:
@interface BKPhotoCell : UICollectionViewCell
@property (nonatomic, strong) UIImageView *imageView;
@end
BKPhotoCell.m:
@interface BKPhotoCell() <UIScrollViewDelegate>
@property (nonatomic ,strong) UIScrollView *scrollView;
@end
@implementation BKPhotoCell
#define isPortrait [[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortrait || [[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortraitUpsideDown
#pragma mark - ScrollView Delegate
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return self.imageView;
}
- (void)scrollViewDidZoom:(UIScrollView *)scrollView
{
CGFloat offsetX = (scrollView.bounds.size.width > scrollView.contentSize.width)?
(scrollView.bounds.size.width - scrollView.contentSize.width) * 0.5 : 0.0;
CGFloat offsetY = (scrollView.bounds.size.height > scrollView.contentSize.height)?
(scrollView.bounds.size.height - scrollView.contentSize.height) * 0.5 : 0.0;
_imageView.center = CGPointMake(scrollView.contentSize.width * 0.5 + offsetX,
scrollView.contentSize.height * 0.5 + offsetY);
}
#pragma mark - View Lifecycle
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.contentView.bounds.size.width, self.contentView.bounds.size.height)];
[self.scrollView setAutoresizingMask:UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth];
[self.scrollView setContentMode:UIViewContentModeScaleAspectFit];
[self.scrollView setDelegate:self];
[self.contentView addSubview:_scrollView];
self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.contentView.bounds.size.width, self.contentView.bounds.size.height)];
[self.imageView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
[self.imageView setContentMode:UIViewContentModeScaleAspectFit];
[self.scrollView addSubview:self.imageView];
}
return self;
}
- (void)prepareForReuse {
[super prepareForReuse];
NSLog(@"Prepare for reuse");
}
- (void)layoutSubviews {
[super layoutSubviews];
[self.imageView setFrame:CGRectMake(0, 0, _imageView.image.size.width, _imageView.image.size.height)];
// NSLog(@"Setting Content Size: %f, %f", _imageView.image.size.width, _imageView.image.size.height);
[_scrollView setContentSize:_imageView.image.size];
CGFloat offsetX = (_scrollView.bounds.size.width > _scrollView.contentSize.width)?
(_scrollView.bounds.size.width - _scrollView.contentSize.width) * 0.5 : 0.0;
CGFloat offsetY = (_scrollView.bounds.size.height > _scrollView.contentSize.height)?
(_scrollView.bounds.size.height - _scrollView.contentSize.height) * 0.5 : 0.0;
_imageView.center = CGPointMake(_scrollView.contentSize.width * 0.5 + offsetX,
_scrollView.contentSize.height * 0.5 + offsetY);
float minimumZoomScale;
if (isPortrait) {
minimumZoomScale = _imageView.frame.size.width >= _imageView.frame.size.height ? _scrollView.frame.size.width / _imageView.frame.size.width : _scrollView.frame.size.height / _imageView.frame.size.height;
} else {
minimumZoomScale = _imageView.frame.size.width > _imageView.frame.size.height ? _scrollView.frame.size.width / _imageView.frame.size.width : _scrollView.frame.size.height / _imageView.frame.size.height;
}
[self.scrollView setMinimumZoomScale:minimumZoomScale];
[self.scrollView setMaximumZoomScale:minimumZoomScale * 2];
[self.scrollView setZoomScale:minimumZoomScale];
}