Search code examples
macoscocoanstableviewnstableviewcell

NStableView Cocoa


I populated my NStableView with tableView Controller and it's working fine. I only want to know why every time I am getting the data (Presented in table Cell) whenever the user hovers on a particular cell in a tableview, it starts displaying the data in console.

I found that this - (NSCell *)tableView:(NSTableView *)tableView dataCellForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row calls every time and I stack traced using instruments and this method is taking a lot of memory.

Is there any way to stop this method drawing the data every time.


Solution

  • Add the delegate -viewWillMoveToWindow to the view subclass contain the table. here i have used the BOOL named reloadTable. NSTrackingArea is the answer for your problem

    - (void) viewWillMoveToWindow:(NSWindow *)newWindow
    {
        // Setup a new tracking area when the view is added to the window.
        NSTrackingArea* trackingArea = [[NSTrackingArea alloc] initWithRect:[yourTable frame]
                    options: (NSTrackingMouseEnteredAndExited |  
                    NSTrackingActiveAlways|NSTrackingEnabledDuringMouseDrag) owner:self userInfo:nil];
        [self addTrackingArea:trackingArea];
    
    }
    
    - (void) mouseEntered:(NSEvent*)theEvent {
        reloadTable=YES;
        NSLog(@"enter %@",theEvent);
    
    }
    
    - (void) mouseExited:(NSEvent*)theEvent {
    
         reloadTable=YES;
    
    }
    

    then use it in your NSTableViewDataSource methods