Re: Displaying an NSArray of NSArray's in a Table view
Re: Displaying an NSArray of NSArray's in a Table view
- Subject: Re: Displaying an NSArray of NSArray's in a Table view
- From: Jonathan Jackel <email@hidden>
- Date: Sun, 12 Dec 2004 13:55:05 -0500
If you make headerCell an NSTableHeaderCell instead of a generic
NSCell, you'll probably get the appearance you desire.
Also, you need to release headerCell once it is set as the column's
header.
Jonathan
On Dec 12, 2004, at 1:03 PM, Bruce Truax wrote:
Thank you for catching that typo. That solved the problem. The data
now
displays. I also added a couple of lines to add column headers. The
code
now looks like:
for (i=0;i<xSize;i++)
{
NSTableColumn *theColumn = [[NSTableColumn alloc] init];
NSCell *headerCell =[[NSCell alloc]init];
[headerCell setTitle:[NSString stringWithFormat:@"%i",i]];
[theColumn setIdentifier:[NSString stringWithFormat:@"%i",i]];
[theColumn setHeaderCell:headerCell];
[displayArrayTable addTableColumn:theColumn];
[theColumn release];
}
This causes headers to display but without the nice Aqua bevel. Also,
as I
scroll left and right the text does not erase in the header so
eventually
there is just a lot of dark pixels in the headers and it is not
possible to
read the numbers. There must be a better way to set the headers.
Bruce
On 12/12/04 12:17 PM, "Jonathan Jackel" <email@hidden>
eloquently
wrote:
You are on the right track, but I suggest reading the "Programming
Topic" on Table Views. That should answer your questions.
<file://localhost/Developer/ADC Reference Library/documentation/
Cocoa/Conceptual/TableView/index.html>
The value returned by your datasource should look something like
[[rows objectAtIndex:rowIndex] objectForKey:[aTableColumn
identifier]];
There seems to be a typo in your code. Don't you mean %i, not i%?
Also, an identifier can be any cocoa object (although you probably
should limit identifiers to objects that conform to the NSCopying
protocol so that they can be used as keys in dictionaries). NSNumbers
work too, but it doesn't make much difference in this context.
Jonathan
On Dec 12, 2004, at 8:36 AM, Bruce Truax wrote:
I have implemented these methods in my datasource object. When I
have
used
table views in the past I have always used bindings and I have bound
each
column to the column of the table. In this case I cannot do that
because
the array is created dynamically and I do not have advance knowledge
of the
columns. It appears that I need to start with an empty table and
then
add
columns dynamically which I do as follows:
for (i=0;i<10;i++)
{
NSTableColumn *theColumn = [[NSTableColumn alloc] init];
[theColumn setIdentifier:[NSString
stringWithFormat:@"i%",i]];
[displayArrayTable addTableColumn:theColumn];
[theColumn release];
}
I set the identifier of each column as a string which is the same as
the
column number. This is also the key that I use in the dictionary
representing the objects in each row. (At your suggestion that table
views
are really set up to deal with arrays of dictionaries.)
I then do the following:
[self setDisplayArray:[[DLDDisplayArray alloc]
initWithCArray:outputArray
withXDimension:xSize
withYDimensiont:ySize]];
[displayArray retain];
[displayArrayTable setDataSource:displayArray];
[displayArrayTable setNeedsDisplay:YES];
I end up with a table containing the correct number of columns (all
with the
column header title "FIELD" but no data in the table. I think some
of
the
problem is related to my column identifiers but the documentation is
unclear
as to how these should be specified. I appears that they should be
the
strings representing the keys for each column but I am not sure.
Bruce
On 12/11/04 6:51 PM, "Jonathan Jackel" <email@hidden>
eloquently
wrote:
Table views are really set up to deal with arrays of dictionaries,
not
arrays of arrays. Also, to understand table views you need to
understand table columns as well. I would spend some time with the
NSTableView and NSTableColumn docs (NSArray and NSMutableArray docs,
too).
I imagine that there is some clever way of making this work with
bindings, but I don't know it. I would use a data source and five
lines of code.
In the table datasource, implement this (written in Mail):
- (int)numberOfRowsInTableView:(NSTableView *)aTableView
{
return [rows count];
}
- (id)tableView:(NSTableView *)aTableView
objectValueForTableColumn:(NSTableColumn *)aTableColumn
row:(int)rowIndex
{
int columnIndex = [[aTableView tableColumns]
indexOfObject:aTableColumn];
return [[rows objectAtIndex:rowIndex]
objectAtIndex:columnIndex];
}
- (void)tableView:(NSTableView *)aTableView
setObjectValue:anObject
forTableColumn:(NSTableColumn *)aTableColumn
row:(int)rowIndex
{
int columnIndex = [[aTableView tableColumns]
indexOfObject:aTableColumn];
[[rows objectAtIndex:rowIndex] replaceObjectAtIndex:columnIndex
withObject:anObject];
}
Jonathan
On Dec 10, 2004, at 8:57 PM, Bruce Truax wrote:
As some of you have seen from an earlier post, I have taken a two
dimensional C array of floats and created an NSMutableArray (rows)
of
NSMutableArrays (columns) of NSNumbers which I created from the C
array of
floats. It seems that this should be easy to display in a table
view
but it
is not obvious how to do this. Binding the content array of the
table
view
to the NSMutableArray does not cause the display, neither does
setting
the
data source.
Suggestions are appreciated.
Bruce
--
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden