Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5992139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:29:19+00:00 2026-05-22T23:29:19+00:00

I have been playing with some of Apple’s example code for customizing UITableViewCells. I

  • 0

I have been playing with some of Apple’s example code for customizing UITableViewCells. I have run into some weird behavior that has left me completely confused about how backgroundColor works.

The following code is a much reduced version of Apple’s example custom UIView within custom UITableViewCell. The init function sets the background color to purple and then the drawRect sets the background color to green. I would expect to never see purple, but that is all I see. Through NSLog statements I know that the init method is being called for each of the cells, followed by drawRect being called for each of the cells. The green setting seems to be ignored. If I call [self setNeedsDisplay] any time after the initial load, the background is correctly set to green.

- (id)initWithFrame:(CGRect)frame {
    counter = 0;
    if ((self = [super initWithFrame:frame])) {
        self.opaque = YES;
        self.backgroundColor = [UIColor purpleColor];
    }
    return self;
}

- (void)drawRect:(CGRect)rect { 
    self.backgroundColor = [UIColor greenColor];
}

Can anyone explain to me why this would be happening like this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T23:29:20+00:00Added an answer on May 22, 2026 at 11:29 pm

    Your -drawRect: implementation doesn’t actually do any drawing, so it’s not going to change anything itself.

    Presumably, the drawing is being done by a layer associated with your view. It looks like, in your environment, the background-colored layer is doing its drawing before your -drawRect: method is called. When the layer redraws, your -drawRect: method also happens to be called. So, after you change the background color, you don’t see any changes till the background layer draws again, which you can tell happened because your -drawRect: gets called again.

    If you want the color change to apply immediately, you need to mark your view as needing display, so that it will completely redraw with the changed background color.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been playing with image flood fill that I found here on Stack
I have been playing around with this and wanted to get a second opinion.
I've been playing with RavenDB recently and there is something that is annoying me
I have been trying this for so long, but was not able to implement
I've been playing around with adding hidden iframe elements to a page, and I
I have a website which I have been working on creating very rapidly, and
I've been trying to upgrade my Java skills to use more of Java 5
TLDR: Does anyone have experience of both protovis & D3.js to illuminate the differences
Possible Duplicate: Android How to draw a smooth line following your finger I'm new

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.