3
votes

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: is called).

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.

Why is 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 results in a 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.

1
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 somewhere and the cell has been replaced (i.e. by scrolling).Eiko
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.Matt Connolly
did you ever find a solution for this?Maggie
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.Matt Connolly

1 Answers

1
votes

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.

Hope that helps!