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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:18:28+00:00 2026-06-16T05:18:28+00:00

I have a few lines of drawing code that are duplicated in two different

  • 0

I have a few lines of drawing code that are duplicated in two different subclasses. When I move this drawing code to its own class and then call it from within drawRect: it is called but it is never drawn to the screen. What is the right way prevent duplicating code in two different drawRect: methods?

Details: I’m making a custom control by subclassing NSTableView and NSTableCellView. My drawing code needs to be in drawRect: in both of these subclasses.

I created a subclass of NSObject that declares one method. Here is the implementation:

@implementation TNLChartDrawingExtras

- (void)drawDividersInRect:(NSRect)rect startingAtDate:(NSDate *)startDate withZoomFactor:(NSNumber *)zoomFactor {

    float pos = 0;
    NSDate *currentDate = [startDate copy];

    while (pos < rect.size.width) {

        //draw the vertical divider
        NSBezierPath *linePath = [NSBezierPath bezierPathWithRect:NSMakeRect(pos, 0.0, 1.0, rect.size.height)];
        [[NSColor colorWithCalibratedWhite:0.85 alpha:0.5] set];
        [linePath fill];

        //increment the values for the next day
        currentDate = [NSDate dateWithTimeInterval:86400 sinceDate:currentDate]; // add one day to the current date
        pos = pos + (86400.0/ [zoomFactor floatValue]);
    }
}

In my NSTableView subclass I define a property for this object. Then in awakeFromNib I create an instance of this class:

- (void)awakeFromNib {
    self.extras = [[TNLChartDrawingExtras alloc] init];
}

In drawRect: I send this message:

- (void)drawRect:(NSRect)dirtyRect {
    // more code here...

    [self.extras drawDividersInRect:viewBounds startingAtDate:chart.startDate withZoomFactor:self.zoomFactor];
}

The code is executed but the lines it is supposed to draw don’t appear. If I put the code from drawDividersInRect:... in the drawRect: method, it works fine.

  • 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-16T05:18:29+00:00Added an answer on June 16, 2026 at 5:18 am

    My original solution (described in the question) may have worked if I had continued to debug it. However, I think the more important question is what is the right way to approach this problem. Here I solve it by adding category on NSView to the project:

    I’m trying to add custom drawing code to both NSTableView and NSTableCellView. Both are subclasses of NSView so I created a category of NSView and added my custom drawing method there. Now I can call my drawing method from both subclasses.

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

Sidebar

Related Questions

I have this line of code: System.Drawing.Icon icon = System.Drawing.Icon.FromHandle(shinfo.hIcon); A few lines later,
I have a few lines of code that start something like this: $trailheads =
i have this few lines of code FilterCondition CustomerID = new FilterCondition(Customer_ID,cust.Customer_ID,FilterCondition.FilterOperator.Equals); List<FilterCondition> conditions
Suppose I have a few lines out of wikipedia XML that looks like this:
I have a few lines of code that should make a report about the
I have a few lines of jQuery in my web application. This code is
I have a few lines of code that I want to run asynchronously in
I have a few lines of code where I use this function, but it
I have a few lines of PowerShell code that I would like to use
I have been fiddling with a few lines of code for two days now

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.