Re: NSTableView and displaying c strings
Re: NSTableView and displaying c strings
- Subject: Re: NSTableView and displaying c strings
- From: Corbin Dunn <email@hidden>
- Date: Mon, 03 Dec 2007 12:58:46 -0800
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
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden