3

I'm trying to determine the best way to use drawRect for something like the cell in the image below and still animate between selected and non-selected states:

Custom UITableViewCell

I've seen a few suggestions, with the most advanced seeming being this one:

But it still seems pretty hacky, so I'm not quite sold on it. Feels like there has to be a cleaner way.

I suppose another option could be having two different UIViews, one that drew the un-selected state, and one that drew the selected state (the latter probably being a subclass of the former with a slightly different draw rect.) Then you could add both of those to contentView, and hide/fade between them? That wouldn't support edit mode or accessories very well, but maybe that's ok?

Or perhaps you follow that same method, but stick them into backgroundView and selectedBackgroundView respectively?

Or if one respects this cell structure, perhaps you truly only draw the background (the tan I guess in the image above) into the backgroundView, a darker tan into the selectedBackgroundView, and draw everything else in a subclass uiview added to contentView? But then you are drawing in a couple different places (creating your own contexts I would imagine), and still would have issues with the text/decorative images not animating, assuming those changed color.

So what is the answer here?

swiftBoy
  • 35,607
  • 26
  • 136
  • 135
Bob Spryn
  • 17,742
  • 12
  • 68
  • 91
  • This isn't the same question. The other one was about core graphics drawing techniques. This question is specific to using drawRect with a selected state. If I had asked it in the other question, you would have said that there were too many questions there. Try reading the actual questions before closing. Clearly this is an important technique to understand when there are entire articles about it: http://blog.giorgiocalderolla.com/2011/04/16/customizing-uitableviewcells-a-better-way/ Please reopen. – Bob Spryn Feb 01 '13 at 17:09
  • @CodaFi please re-read both questions. They are very different. This one is specific to the best handling of selected state when using drawRect while the other is specific to how to draw more complicated cells. – Bob Spryn Feb 01 '13 at 17:48
  • Answer: You need to track state, then in your drawRect see what the state of the selection is and draw based on it. When user actions happen you'll have to redisplay the UIView component so that it redraws. – Paul Solt Feb 01 '13 at 22:16
  • Oh dear. You called an orang-utan a monkey. Run! Run far and quick! The Librarian is coming to get you. Ook! – TRiG Feb 02 '13 at 01:02

0 Answers0