【发布时间】:2015-01-28 19:44:46
【问题描述】:
我使用UICollectionView 类设置了3x18“网格视图”。每个单元格都包含一个从NSDictionary 加载的UIImageView。共有 18 张图片,总计约“20 MB”。
我遇到的问题是当我最初在视图中时,我从顶部滚动到底部,它很滞后。之后,滚动就像照片应用一样流畅。
为什么会这样?是不是因为我的图片太大了?
这是我的代码:
- (void)viewDidLoad
{
[super viewDidLoad];
dictionary = @{@"01" : @[@"Image 1"],
@"02" : @[@"Image 2"],
// etc etc
};
numOfImages = [ [dictionary allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
// Allows appropriate methods to be used
self.pageantCollectionView.delegate = self;
self.pageantCollectionView.dataSource = self;
}
// Populate each cell with specified data
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
NSString *number = [numOfImages objectAtIndex:indexPath.row];
NSArray *imageByNumber = [dictionary objectForKey:number];
NSString *name= [imageByNumber objectAtIndex:indexPath.section];
cell.nameLabel.text = name;
cell.imageView.image = [UIImage imageNamed:[self getImage:name] ];
// Set rounded corners
CALayer * l = [cell.imageView layer];
[l setMasksToBounds:YES];
[l setCornerRadius:10.0];
return cell;
}
- (void)collectionView:(UICollectionView *)colView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell* cell = [colView cellForItemAtIndexPath:indexPath];
CALayer * l = [cell layer];
[l setMasksToBounds:YES];
[l setCornerRadius:10.0];
cell.contentView.backgroundColor = [UIColor lightGrayColor];
}
- (void)collectionView:(UICollectionView *)colView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell* cell = [colView cellForItemAtIndexPath:indexPath];
cell.contentView.backgroundColor = nil;
}
这是否与我在cellForItemAtIndexPath 内动态设置“imageView”的角半径以及didHighlightItemAtIndexPath 内单元格本身的角半径这一事实有关?
如果是这样,我该如何为“imageView”和“cell”本身静态创建圆角半径?
谢谢
【问题讨论】:
标签: ios objective-c uicollectionview uicollectionviewcell