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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:04:27+00:00 2026-05-15T21:04:27+00:00

in my cocoa application, I need a custom NSCell for an NSTableView. This NSCell

  • 0

in my cocoa application, I need a custom NSCell for an NSTableView. This NSCell subclass contains a custom NSButtonCell for handling a click (and two or three NSTextFieldCells for textual contents). You’ll find a simplified example of my code below.

@implementation TheCustomCell

- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
   // various NSTextFieldCells
   NSTextFieldCell *titleCell = [[NSTextFieldCell alloc] init];
   ....
   // my custom NSButtonCell
   MyButtonCell *warningCell = [[MyButtonCell alloc] init];
   [warningCell setTarget:self];
   [warningCell setAction:@selector(testButton:)];
   [warningCell drawWithFrame:buttonRect inView:controlView];
}

The problem I’m stuck with is: what is the best/right way to get that Button (more precisely: the NSButtonCell) inside this NSCell to work properly? “work” means: trigger the assigned action message and show the alternate image when clicked. Out of the box, the button doesn’t do anything when clicked.

Information / readings on this topic is hard to find. The only posts I found on the net pointed me to implementing

- (BOOL)trackMouse:(NSEvent *)theEvent inRect:(NSRect)cellFrame ofView:(NSView *)controlView untilMouseUp:(BOOL)untilMouseUp; 

Is this the correct way to do it??? Implement trackMouse: in my containing NSCell? And then forward the event to the NSButtonCell? I would have expected the NSButtonCell itself to know what to do when it’s being clicked (and I saw the trackMouse: methods more in cunjunction with really tracking mouse movements – not as a training wheel for ‘standard’ click behaviour). But it seems like it doesn’t do this when included in a cell itself…
It seems I haven’t grasped the big picture on custom cells, yet 😉

I’d be glad if someone could answer this (or point me to some tutorial or the like) out of his own experience – and tell me if I’m on the right track.

Thanks in advance,
Tobi

  • 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-15T21:04:27+00:00Added an answer on May 15, 2026 at 9:04 pm

    The minimal requirements are:

    • After left mouse down on the button, it must appear pressed whenever the mouse is over it.
    • If the mouse then releases over the button, your cell must send the appropriate action message.

    To make the button look pressed, you need to update the button cell’s highlighted property as appropriate. Changing the state alone will not accomplish this, but what you want is for the button to be highlighted if, and only if, its states is NSOnState.

    To send the action message, you need to be aware of when the mouse is released, and then use -[NSApplication sendAction:to:from:] to send the message.

    In order to be in position to send these messages, you will need to hook into the event tracking methods provided by NSCell. Notice that all those tracking methods, except the final, -stopTracking:... method, return a Boolean to answer the question, “Do you want to keep receiving tracking messages?”

    The final twist is that, in order to be sent any tracking messages at all, you need to implement -hitTestForEvent:inRect:ofView: and return an appropriate bitmask of NSCellHit... values. Specifically, if the value returned doesn’t have the NSCellHitTrackableArea value in it, you won’t get any tracking messages!

    So, at a high level, your implementation will look something like:

    - (NSUInteger)hitTestForEvent:(NSEvent *)event
                           inRect:(NSRect)cellFrame
                           ofView:(NSView *)controlView {
        NSUInteger hitType = [super hitTestForEvent:event inRect:cellFrame ofView:controlView];
    
        NSPoint location = [event locationInWindow];
        location = [controlView convertPointFromBase:location];
        // get the button cell's |buttonRect|, then
        if (NSMouseInRect(location, buttonRect, [controlView isFlipped])) {
            // We are only sent tracking messages for trackable areas.
            hitType |= NSCellHitTrackableArea;
        }
        return hitType;
    }
    
    + (BOOL)prefersTrackingUntilMouseUp {
       // you want a single, long tracking "session" from mouse down till up
       return YES;
    }
    
    - (BOOL)startTrackingAt:(NSPoint)startPoint inView:(NSView *)controlView {
       // use NSMouseInRect and [controlView isFlipped] to test whether |startPoint| is on the button
       // if so, highlight the button
       return YES;  // keep tracking
    }
    
    - (BOOL)continueTracking:(NSPoint)lastPoint at:(NSPoint)currentPoint inView:(NSView *)controlView {
       // if |currentPoint| is in the button, highlight it
       // otherwise, unhighlight it
       return YES;  // keep on tracking
    }
    
    - (void)stopTracking:(NSPoint)lastPoint at:(NSPoint)stopPoint inView:(NSView *)controlView mouseIsUp:(BOOL)flag {
       // if |flag| and mouse in button's rect, then
       [[NSApplication sharedApplication] sendAction:self.action to:self.target from:controlView];
       // and, finally,
       [buttonCell setHighlighted:NO];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my Cocoa Application I have created a Custom View Class (subclass of NSView),
I have a Cocoa application that records datestamps on events. I need to know
I'm currently porting my jruby/java2d Graph Drawing/Layouting application to macruby/cocoa. Therefore I need to
In my Cocoa application I need to run a task that uses unstable unfixable
I need to use a library in a Cocoa application and can use either
I'm writing a Cocoa application, which uses NSURLs -- I need to remove the
I need to create a cocoa application, that run in back ground. I used
I need to display informative message box in Cocoa Application, which control should i
i need a simple logging solution while writing Cocoa/OpenGL application in C++. I'd like:
My OSX Cocoa application uses SQLite to manage data. When initializing, I need to

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.