NSTableView and displaying c strings

  • I'm a Cocoa newbie trying to port my Carbon app to Cocoa for 64-bit
    GUI support so forgive my ignorance.

    I've got data in memory and I want to display the variable names of
    the data (as well as other properties) which are stored as c strings
    (in the data) in an NSTableView.  The data is operated on by code
    common to all the platforms we support (Mac, Windows, Unix) so I can't
    change the format of it.  Maintaining the properties of my data in an
    array of NSStrings is not practical because the properties can change
    at any moment (there can be up to 32,000 variables) and I'm only
    notified that it has changed, not what has changed.  It's also
    wasteful since the values are already there, just in the wrong format.

    What I did for my tableView:objectValueForTableColumn:row: method is
    convert the c string for a given column and row to an NSString and
    return it (my simplified example assumes just one column).

    - (id) tableView:(NSTableView *) tableView
    objectValueForTableColumn:(NSTableColumn *) tableColumn
    row:(int) row
    {
    NSString *string;

    // VarName() is a wrapper for returning the pointer to a null-
    terminated c string given an index
    string = [[NSString alloc] initWithCString:VarName(row)
      encoding:NSMacOSRomanStringEncoding];
    return(string);
    }

    I assume I have to release the NSStrings but I don't have any
    references to them so I subclassed NSTextFieldCell with my own
    drawWithFrame which draws the cell then releases the strings.

    - (void) drawWithFrame:(NSRect) cellFrame inView:(NSView *) controlView
    {
    [super drawWithFrame:cellFrame inView:controlView];
    [[self objectValue] release];
    }

    My question is is this a safe and or a correct thing to do?

    The only other thing I can think of is to use an array of row indices
    (NSNumber) as my data source and write custom NSFormatters for each
    column of my NSTableView that can convert the indices to the strings I
    need.

    Also, I need click-through support on my NSTableView so I subclassed
    it and return YES from acceptsFirstMouse.  This doesn't have any
    effect.  What do I need to do to get click-through support?

    Also2, I've seen rendering problems in my NSTableView (stray
    horizontal scrollbar and header corner lines not erased) when it's
    resized (it resizes with its parent window).  I even see it when using
    IB to test out an NSTableView.  Is this a known issue?

    -Chinh Nguyen
      <cnguyen...>
  • On Nov 30, 2007, at 3:23 PM, Chinh Nguyen wrote:

    > What I did for my tableView:objectValueForTableColumn:row: method is
    > convert the c string for a given column and row to an NSString and
    > return it (my simplified example assumes just one column).
    >
    > // VarName() is a wrapper for returning the pointer to a null-
    > terminated c string given an index
    > string = [[NSString alloc] initWithCString:VarName(row)
    > encoding:NSMacOSRomanStringEncoding];
    > return(string);
    > }

    You can use [NSString stringWithCString:encoding] instead of [NSString
    initWithCString:encoding] and the returned string will be autoreleased
    (that is, released "later") and then you won't have to worry about the
    reference count.

    And while you can't change the format of the code today, you may want
    to consider a plan to migrate your data to UTF-8 at the least (or
    perhaps it already is and your just using the wrong encoding above?).
    --
    David Duncan
    Apple DTS Animation and Printing
    <david.duncan...>
  • On Nov 30, 2007, at 3:23 PM, Chinh Nguyen wrote:

    > I'm a Cocoa newbie trying to port my Carbon app to Cocoa for 64-bit
    > GUI support so forgive my ignorance.
    >
    > I've got data in memory and I want to display the variable names of
    > the data (as well as other properties) which are stored as c strings
    > (in the data) in an NSTableView.  The data is operated on by code
    > common to all the platforms we support (Mac, Windows, Unix) so I
    > can't change the format of it.  Maintaining the properties of my
    > data in an array of NSStrings is not practical because the
    > properties can change at any moment (there can be up to 32,000
    > variables) and I'm only notified that it has changed, not what has
    > changed.  It's also wasteful since the values are already there,
    > just in the wrong format.
    >
    > What I did for my tableView:objectValueForTableColumn:row: method is
    > convert the c string for a given column and row to an NSString and
    > return it (my simplified example assumes just one column).
    >
    > - (id) tableView:(NSTableView *) tableView
    > objectValueForTableColumn:(NSTableColumn *) tableColumn
    > row:(int) row
    > {
    > NSString    *string;
    >
    > // VarName() is a wrapper for returning the pointer to a null-
    > terminated c string given an index
    > string = [[NSString alloc] initWithCString:VarName(row)
    > encoding:NSMacOSRomanStringEncoding];
    > return(string);
    > }
    >
    > I assume I have to release the NSStrings but I don't have any
    > references to them so I subclassed NSTextFieldCell with my own
    > drawWithFrame which draws the cell then releases the strings.
    >
    > - (void) drawWithFrame:(NSRect) cellFrame inView:(NSView *)
    > controlView
    > {
    > [super drawWithFrame:cellFrame inView:controlView];
    > [[self objectValue] release];
    > }
    >
    > My question is is this a safe and or a correct thing to do?

    This will cause many problems and difficult to debug things. As david
    suggested, use an autoreleased NSString, and don't do the override in
    drawWithFrame:. Everything will work fine, and you won't have any
    memory leaks.

    --corbin

    >
    >
    > The only other thing I can think of is to use an array of row
    > indices (NSNumber) as my data source and write custom NSFormatters
    > for each column of my NSTableView that can convert the indices to
    > the strings I need.
    >
    > Also, I need click-through support on my NSTableView so I subclassed
    > it and return YES from acceptsFirstMouse.  This doesn't have any
    > effect.  What do I need to do to get click-through support?
    >
    > Also2, I've seen rendering problems in my NSTableView (stray
    > horizontal scrollbar and header corner lines not erased) when it's
    > resized (it resizes with its parent window).  I even see it when
    > using IB to test out an NSTableView.  Is this a known issue?

    There's one issue with this that I know of, bug please log a bug
    reporting it (and the more precise your bug report is, the better it
    is for us to rapidly fix the problem. Code samples or projects
    attached to the bug are very helpful).

    -corbin
previous month december 2007 next month
MTWTFSS
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31            
Go to today