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 9214941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:05:15+00:00 2026-06-18T02:05:15+00:00

I have custom UITableViewCell class which implements custom drawn design (as you can see,

  • 0

I have custom UITableViewCell class which implements custom drawn design (as you can see, the cell has shadow effect.)
As you can see below I’m trying to paint 1st cell with white color, any other cell must be gray.

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

CGColorRef whiteColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0].CGColor;
if (_rowNumber == 0)
    CGColorRef lightColor = [UIColor whiteColor].CGColor;
else
    CGColorRef lightColor = [UIColor grayColor].CGColor;
CGColorRef darkColor = _darkColor.CGColor;
CGColorRef shadowColor = [UIColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:0.5].CGColor;
CGContextSetFillColorWithColor(context, whiteColor);

CGContextFillRect(context, _paperRect);
CGContextSaveGState(context);

CGContextSetShadowWithColor(context, CGSizeMake(0, 2), 7.0, shadowColor);
CGContextSetFillColorWithColor(context, lightColor);

CGContextFillRect(context, _coloredBoxRect);
CGContextRestoreGState(context); 
}

The problem is: when I’m using dequeueReusableCellWithIdentifier the method “drawRect” invoked only 3 times (my cells have height 200), so the fourth cell has same color like the first one.

Is there any way to manage this problem?
Thanks

UPDATE
i’ve tried to invoke my custom redraw method from UITableViewController, but i have got “null” context error

- (void)refreshColorOfRow:(int)row{
 CGContextRef context = UIGraphicsGetCurrentContext();
 if (row > 0)
     _lightColor = [UIColor colorWithRed:200.0f/255.0f green:199.0f/255.0f blue:200.0f/255.0f alpha:1.0];
 else
     _lightColor = [UIColor colorWithRed:240.0f/255.0f green:240.0f/255.0f blue:240.0f/255.0f alpha:1.0];
CGColorRef lightColor = _lightColor.CGColor;
CGContextSetFillColorWithColor(context, lightColor);
CGContextFillRect(context, _coloredBoxRect);
CGContextRestoreGState(context);
}
  • 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-06-18T02:05:16+00:00Added an answer on June 18, 2026 at 2:05 am

    In -(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath you could call [cell setNeedsDisplay];.

    But why don’t you use [cell setBackgroundColor:[UIColor yourColorXY]]; if you only want some simple colors and shadows?

    EDIT:
    Like Guo Luchuan said, is an often called drawRect: a performance-killer.
    Just try it simple 😉

    NSIndexPath *selectedCell; //Declare this in your .h
    
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        static NSString *CellIdentifier = @"CellIdentifier";
        UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier: CellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: CellIdentifier];
        }
        if (selectedCell.section==indexPath.section && selectedCell.row==indexPath.row) {
            [cell.contentView setBackgroundColor:[UIColor whiteColor]];
            [cell.textLabel setText:@"The Choosen One"];
        } else {
            [cell.contentView setBackgroundColor:[UIColor lightGrayColor]];
            [cell.textLabel setText:@"FOO"];
        }
        return cell;
    }
    
    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        selectedCell=indexPath;
        [tableView reloadData];
    }
    

    And for the shadows you can play around with..

    #import <QuartzCore/QuartzCore.h>
    
    CALayer *layer=[cell layer]; //Or [tableView layer] or whatever you need
    [layer setShadowColor:[[UIColor lightGrayColor] CGColor]];
    [layer setShadowOffset:CGSizeMake(1, 1)];
    [layer setShadowOpacity:1.0];
    [layer setMasksToBounds:NO];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a custom UITableViewCell which has an image & and some text. When
I have a created custom table view cell class named CustomCell which inherits from
I am making a custom uitableviewcell class and inside this cell I have a
I have a custom table cell class. TextInput.h @interface TextInput : UITableViewCell <UITextFieldDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate> {
I have a custom tableview cell which has a button. I need to pass
I have a custom view class which is a subclass of UITableViewCell. I have
I have custom UITableViewCell . It contains UITextLabel . When I press this cell
I have a custom UITableViewCell subclass. I have set the contentView of my cell
I have a custom UITableViewCell which contains several UIButtons. Each button's frame position is
I have created a UITableView with a custom UITableViewCell . My cell includes one

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.