- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个带有自定义 UICollectionViewCell 类的 UICollectionView,我在其中绘制了必要的内容。然而,我发现当我上下滚动 Collection View 时,它会重新绘制每个单元格,因为它会再次滚动并返回到屏幕上。这会导致相当不稳定的滚动。我认为 UICollectionViewCells 是自动缓存和重用的,所以我不明白为什么它会不断重绘它们。
我在自定义 UICollectionViewCell 的 subview 的 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 是我创建的用于显示图像的自定义 View ,我在其他各种地方使用它:
- (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;
}
最佳答案
您应该将初始化语句放在 initWithFrame: 方法中。
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
MiniView *miniView = [[MiniView alloc] initWithFrame:rect];
miniView.mini = _mini;
[self addSubview:miniView];
UIImageView *backgroundImage = [[UIImageView alloc] initWithImage:[self randomBackground]];
self.backgroundView = backgroundImage;
}
return self;
}
我不记得在没有实际绘制内容的情况下在 drawRect: 方法中完成了初始化。如果您想实际绘制单元格的内容,您应该使用图形上下文:
UIGraphicsBeginImageContext(self.bounds.size);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *imageOfContextSnapshot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
这将获取当前上下文的“快照”。您可以将其视为将对象粘贴到 Canvas 上。在您的 drawRect: 方法中调用它应该绘制您的内容。请注意,您应该只更新 drawRect: 方法中的上下文,否则您将收到错误消息。此外,只有在实际调用绘图方法时才使用 drawRect: 方法。当创建一个新的 UIView 类时,drawRect: 方法甚至声明只有在绘制内容时才应使用该方法。
有关更多信息,请查看 Apple 的引用资料。他们对图形上下文及其使用方法进行了非常详尽的解释。
编辑 1:
根据单元格的数量,您可能需要考虑不绘制任何内容。 dequeueReusableCellWithIdentifier 已经回收单元格内容并且做得很好。您应该根据需要同时增加复杂性。虽然这对我来说很容易说,因为我不知道 MiniView 在做什么。
此外,如果您想要更好的性能,您可能需要考虑特定于对象的绘制方法,而不是使用 renderInContext:。例如,NSSString 有:
-drawAtPoint:forWidth:withFont:
-drawAtPoint:forWidth:withFont:
与渲染整个上下文相比,这些在绘制文本方面表现更好。要绘制图像,您应该调用:
-drawInRect:
这些必须在图形上下文中调用,在您的情况下是在 drawRect: 方法中。
编辑 2:
在init方法中初始化时不绘制mini,因为当你调用
PFObject *pfMini = [_searchResults objectAtIndex:indexPath.row];
Mini *mini = [Mini instanceWithPFObject:pfMini];
cell.mini = mini;
cell.backgroundColor = [UIColor grayColor];
在您的 View Controller 中,迷你 View 已被初始化并作为 subview 添加到单元格中,但是当分配迷你 View 的迷你 iVar 时,单元格的迷你 iVar 很可能为 nil。换句话说,当您在 initWithFrame:(CGRect)frame 方法中调用它时:
MiniView *miniView = [[MiniView alloc] initWithFrame:rect];
miniView.mini = _mini;
[self addSubview:miniView];
此时“_mini”为零。在您的 cellForItemAtIndexPath: 方法中,您将分配单元格的 mini,而不是 MiniView 的 mini,因此 MiniView 的 mini 继续为 nil。您可以在 cellForItemAtIndexPath: 中分配 MiniView 的 mini,而不是单元格的 mini,这应该可以解决它。
关于ios - UICollectionView 在 UICollectionViewCells 上调用 setNeedsDisplay,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20349253/
在我的代码中,我想要“动画”绘制一条线的延迟,因此在向 View 添加新线后,我调用 setNeedsDisplay - 一次就可以正常工作。 在drawRect方法中,我绘制线条并调用线条的方法来增
我正在尝试使用 MonoTouch 创建自定义 ProgressBar。我创建了一个用计时器重新绘制线条的类。我的问题是,当调用 SetNeedsDisplay 时, View 不会自行重绘。仅当提供
我注意到 Mac OS X 10.7 上基于 Cocoa 的应用程序存在一个奇怪的问题。由于某种原因(这里并不重要),有时我必须在自定义 View 的 drawRect 方法之外进行绘制。 我必须在我
我正在运行一个 Parse 程序,该程序获取数据并使用“findObjectsInBackgroundWithBlock”从中创建对象。完成后,我更新 self ,然后调用“[self.tableVi
每次使用 setNeedsDisplay 调用 drawRect 时,我都会显示大量图像。问题是导致滞后导致每次我调用 dracRect 时,我都会重新绘制每个图像(数千张)。 有没有办法不重绘所有图
下面的方法是在非主线程上调用的,具体来说,是在录音音频队列回调中 - (void)myMethod { //... dispatch_async(dispatch_get_main_queue(),
我最近开始学习在 View 上绘制核心图形,但每次调用 setNeedsDisplay 时,该方法都会运行,但不会触发 drawRect。在我的项目中,我在 View Controller 上有一个
我有一个自定义 View ,其中绘制了一个钢琴键盘。每个键都作为单独的调用绘制,因此我只能绘制需要重绘的键。该 View 支持多点触控,因此可以同时按住多个键。 每个键的绘制成本都有些高,因此每当在
在我的程序中,我使用 slider 更改当前所选颜色的 alpha 级别。 当 slider 更改其值时,它调用我的 setColor() 方法来更新使用新的 alpha 值更新当前颜色并更新显示当前
我正在 UIContainerView 中创建 subview ,它属于下面描述的类。这些值正确地打印到终端窗口,但实际上并没有显示在我定义的 subview 中。我认为 setNeedsDispla
我正在尝试重新加载 UIView。实际上我得到一个错误 Cannot convert value of type 'HomeViewController' to expected argument t
我对 setNeedsDisplay 更新 UIView 的不同用法感到困惑?当我尝试使用: [self.view setNeedsDisplay:YES]; 我发现我必须忽略参数才能使其适用于 UI
这里有人知道 setNeedsDisplay 是否会立即调用 drawRect 并立即更新屏幕吗?或者它只是一个排队的请求?谢谢。 最佳答案 View 直到下一个绘图周期才真正重绘。这只是通知系统应该
我看到很多与此问题相关的线程,但没有一个解决我的问题(我认为)。 我的情况应该很简单,我的 Controller 中有一个自定义 UIView,我在我的 Controller 中使用了 [self.m
使用drawRect、setNeedsDisplay和layoutSubViews等方法有什么基本规则吗?我相信它们会对应用程序性能产生不利影响。这些消息有任何首选替代方案吗? 最佳答案 虽然这不是
我有一个更新 View 的后台任务。该任务调用 -setNeedsDisplay 来绘制 View 。 这有效: - (void) drawChangesTask; { NSAutorelea
我的 ui 结构类似于 NSWindow -> NSViewController -> NSView(parent) -> NSView(target view)。目标 View 是从 XIB 文件中
我正在尝试制作一个非常简单的绘画应用程序(Cocoa)。在我的自定义 View 中,我有我的drawRect:方法,它在光标位置创建一个矩形: - (void)drawRect:(NSRect)rec
我有一个仅绘制的 UIView 派生 View 一次并且从未更新。 调用[self setNeedsDisplay]没有效果。什么可能导致这种情况? 绘图代码是否在无关紧要 drawRect: , 或
我正在尝试创建一个循环进度指示器。不幸的是,仅从 setNeedsDisplay 第一次和最后一次调用 drawRect 例程,它不会创建我正在寻找的渐变填充模式。我创建了一个 UIView 子类,在
我是一名优秀的程序员,十分优秀!