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

  • SEARCH
  • Home
  • 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 4564170
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T18:27:46+00:00 2026-05-21T18:27:46+00:00

I have a UITableView where I am trying to get the best possible scrolling

  • 0

I have a UITableView where I am trying to get the best possible scrolling performance.

The cell does not contain any subviews, all is done in the drawRect method.

Each cell has a white line at the top, a grey line at the bottom and a linear gradient between top and bottom. On top of this background is some text and an image.

I can see from using instruments that the main portion (>50%) of the runtime when scrolling comes from the drawing of the linear gradient. Actually almost all of that comes from the call to CGContextDrawLinearGradient.

Is there a good way to optimize this? I am thinking, either cache the linear gradient some way, or maybe draw it in another way… Maybe draw a one pixel wide and then stretch it?

Here is some of my code:

- (void)drawRect:(CGRect)rect  
{  
  [self drawCellBackground:rect];  
  [self drawSeparator:rect];  
  ....
}

- (void)drawCellBackground:(CGRect)rect
{
  CGContextRef context = UIGraphicsGetCurrentContext();

  CGColorRef notQuiteWhiteColor = [UIColor colorWithRed:245.0/255.0 green:245.0/255.0 
                                             blue:245.0/255.0 alpha:1.0].CGColor;
  CGColorRef lightGrayColor = [UIColor colorWithRed:235.0/255.0 green:235.0/255.0 
                                             blue:235.0/255.0 alpha:1.0].CGColor;

  drawLinearGradient(context, rect, notQuiteWhiteColor, lightGrayColor);
}

From a separate .c file:

void drawLinearGradient(CGContextRef context, CGRect rect, CGColorRef startColor, CGColorRef  endColor)
{
  CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
  CGFloat locations[] = { 0.0, 1.0 };

  NSArray *colors = [NSArray arrayWithObjects:(id)startColor, (id)endColor, nil];

  CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, 
                                                  (CFArrayRef) colors, locations);

  CGPoint startPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMinY(rect));
  CGPoint endPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMaxY(rect));

  CGContextSaveGState(context);
  CGContextAddRect(context, rect);
  CGContextClip(context);
  CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0);
  CGContextRestoreGState(context);

  CGGradientRelease(gradient);
  CGColorSpaceRelease(colorSpace);
}
  • 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-21T18:27:47+00:00Added an answer on May 21, 2026 at 6:27 pm

    If you absolutely want to stick to using drawRect: (which I believe gives better performance than separate views on older devices), why not prerender your gradient into a CGImage or UIImage, then use that to draw in -drawRect: ?

    Also, you can create your gradient ahead of time:

    static CGGradientRef GetCellBackgroundGradient()
    {
        static CGGradientRef gradient = NULL ;
        if ( !gradient )
        {
            gradient = // create your CGGradientRef here
        }
    
        return gradient;
    }
    

    For high performance code you want to preallocate as many things (memory/objects) as possible.

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

Sidebar

Related Questions

I'm trying to get rid of a cached UITableView cell. I have two sections.
I have a grouped UITableView in my code and am trying to get the
I'm currently trying to get a UITableView to use custom cell heights based on
I have a UITableView that I am placing inside a TabView. I am trying
I have 3 UITextFields in a grouped UITableView and am trying to figure out
I have a UITableView cell that is going to have a variable size depending
I have a UITableView that displays single large images in each cell. The names
I have a uitableview that loads fairly large images in each cell and the
I am trying to get the sections and index for UITableView from Sqlite (I
I have an interesting problem. I am loading a UITableView asynchronously. I am trying

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.