【发布时间】:2013-12-03 10:58:28
【问题描述】:
我有一个带有自定义 UICollectionViewCell 类的 UICollectionView,我在其中绘制了必要的内容。然而,我发现当我上下滚动集合视图时,它会重新绘制每个单元格,因为它会滚动并再次回到屏幕上。这会导致非常不稳定的滚动。我认为 UICollectionViewCells 会自动缓存和重用,所以我不明白为什么它会不断重绘它们。
我在自定义 UICollectionViewCell 的子视图的 setNeedsDisplay 中有一个 NSLog 语句,这就是为什么我知道它正在重绘它们。这是我用于绘制信息的 UICollectionView 代码。 (PFObject 只是一个使用 Parse 从数据库中检索的对象)
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
MiniCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MiniCell" forIndexPath:indexPath];
if (!cell.mini) {
PFObject *pfMini = [_searchResults objectAtIndex:indexPath.row];
Mini *mini = [Mini instanceWithPFObject:pfMini];
cell.mini = mini;
cell.backgroundColor = [UIColor grayColor];
}
return cell;
}
还有我的自定义单元格。这里的 MiniView 是我创建的用于显示图像的自定义视图,我在其他各种地方都使用它:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
- (void)drawRect:(CGRect)rect
{
[super drawRect:rect];
MiniView *miniView = [[MiniView alloc] initWithFrame:rect];
miniView.mini = _mini;
[self addSubview:miniView];
UIImageView *backgroundImage = [[UIImageView alloc] initWithImage:[self randomBackground]];
self.backgroundView = backgroundImage;
}
- (UIImage *)randomBackground
{
int backgroundInt = arc4random()%26 + 8;
UIImage *background = [UIImage imageNamed:[NSString stringWithFormat:@"MainBackground%i", backgroundInt]];
return background;
}
【问题讨论】:
-
您的第二个来自 UICollectionViewCell 的代码清单是否有问题?
-
没错,就是继承自 UICollectionViewCell 的 MiniCell 类
标签: ios iphone objective-c uicollectionview uicollectionviewcell