¿Por qué mi subclase UIView no se vuelve a dibujar después de llamar a setNeedsDisplay?

I have a custom UIView which is a sub-view of a UITableViewCell. When the cell is drawn, so is my custom view, correctly calling drawRect: in my UIView sub-class. When the cell is reloaded, the view is drawn correctly (drawRect: se llama).

When a certain event happens (eg timer), I want to redraw the view without having to reload the whole cell. (This is not the only view in the cell.)

However, when I call setNeedsDisplay on my sub-view, nothing happens.

I'm guessing that when my view's drawRect: is called, the resulting image is cached in a backing somewhere such that when I call setNeedsDisplay that backing is redrawn to the screen, but my drawRect: call is never called again.

¿Por qué es drawRect: never called again after the cell is created?

EDIT: The cell's subviews are created from from a nib. ie: a UIViewController is loaded from the nib and its view is added to the UITableViewCell.

I made a test case from scratch with a custom view that isn't loaded from a nib (created by code) and it works as expected: setNeedsDisplay resulta en un drawRect: call. It must be something that's either set up in the .xib file or something that happens differently to a view when it's loaded from a nib.

preguntado el 09 de enero de 11 a las 10:01

Do you call setNeedsDisplay from the main thread? Are you sure you are sending it to the correct view (i.e. should be fine if done from the cell, but could easily be done wrong when done en alguna parte and the cell has been replaced (i.e. by scrolling). -

Yes, setNeedsDisplay is being called from the main thread. I've even tried deferring it using performSelectorOnMainThread:... no joy. See the edit about the view coming from the nib(.xib) file. -

¿Alguna vez encontraste una solución para esto? -

I never found the cause. I changed my code to create the view programmatically instead of getting it from a nib/xib file, and haven't revisited the issue since. -

1 Respuestas

I just ran into the same problem myself. For me it was an issue with my XIB - I am subclassing a UIView though - so adapt accordingly. My mistake in IB was that I attached my controls to the "file's owner" instead of attaching them to the UIView. I'm sure that in my inexperience there is something larger going on - perhaps someone with a bit more experience can elaborate.

Espero que ayude!

contestado el 13 de mayo de 11 a las 11:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.