setNeedsDisplay不总是调用drawRect [英] setNeedsDisplay doesn't always call drawRect

查看:107
本文介绍了setNeedsDisplay不总是调用drawRect的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在自定义表格单元格中有自定义视图。每次在自定义视图上的特定属性改变我调用 [self setNeedsDisplay] 其中重绘视图中 - (void)drawRect:(CGRect) rect 。该属性在表视图委托的 tableView:cellForRowAtIndexPath:中设置。但是当表视图大于屏幕并且单元格必须重新使用时, drawRect 不会每次调用 setNeedsDisplay 是。特别是当我快速扑了一下桌子。慢滚动工作正常。这导致第一个和最后一个单元格中的信息通常不正确。

I have a custom view in a custom table cell. Every time a specific property on the custom view is changed I call [self setNeedsDisplay] which redraws the view in - (void)drawRect:(CGRect)rect. That property is set in the table view delegate's tableView:cellForRowAtIndexPath:. But when the table view is larger than the screen and cells have to be reused, drawRect isn't called every time setNeedsDisplay is. Especially when I flick the table quickly . Slow scrolling works fine. This leads to the information in the first and last cells often being incorrect.

在日志中我可以看到,对于 setNeedsDisplay 有一个调用 drawRect ,但是当我快速滚动表视图有更多的调用 setNeedsDisplay drawRect 。当然,这里应该有一对一的比例。

In the log I can see that normally, for every call to setNeedsDisplay there is a call to drawRect, but when I scroll the table view quickly there are more calls to setNeedsDisplay than drawRect. Surely there should be a one-to-one ratio here.

我在普通的UIView中使用相同的自定义视图,每次调用 setNeedsDisplay 。问题似乎孤立于表格视图和重用单元格。

I use the same custom view in a normal UIView, and it redraws perfectly every time I call setNeedsDisplay. The problem seems isolated to table views and reusing cells.

有人知道这里发生了什么吗?
有没有其他方法强制自定义视图重绘本身?

Does anyone know what's going on here? Is there any other way to force the custom view to redraw itself?

推荐答案

我认为我努力解决这个问题几乎一个星期前我发现了问题。这里是,我不小心你不是:

I think I struggled with this issue for almost a week before I found the problem. Here it is, and I kid you not:

指向这个自定义视图的指针是在.m文件中定义的,而不是.h文件,使它成为一个类变量而不是一个实例变量。

The pointer to this custom view was defined in the .m file instead of the .h file, making it a class variable instead of an instance variable.

是的,我很非常尴尬。

这篇关于setNeedsDisplay不总是调用drawRect的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆