custom NSTableView

honkFactory
Unregistered
 
Post: #1
Howdy,
I was wondering how to customize an nstableview. I would like all the row that exceed the value return by the method numberOfRowsInTableView implemented by the NSTableView's data source to be darkened. Currently, rows that exceed this value cannot be selected however they do not appear visually different. How would I do this? If the answer is to subclass NSTableView, what would be the appropriate method to override?
Thanks
update second question about editing cells below
Quote this message in a reply
Member
Posts: 78
Joined: 2002.06
Post: #2
Rows that exceed numberOfRowsInTableView should not be displayed and it sounds like you are missing a method call of reloadData to the table.

If you want to make rows different look at subclassing NSTableColumn to provide the method - (id)dataCellForRow: (int)row.
Quote this message in a reply
honkFactory
Unregistered
 
Post: #3
Hmm, it is still displays rows the exceed the what the datasource indicates, even though my code is positively dripping with reloadData calls (some of which are no doubt superfluous). By displaying rows I mean that it still draws the grid and alternating blue and white background (if I have it activated). I would like the row beyond those specified by the data source to be dimmed out. I will look at NSTableColumn.
Quote this message in a reply
Member
Posts: 78
Joined: 2002.06
Post: #4
Oh ok. Your table isn't displaying old data in rows, but the alternate row highlighting. There is no way to stop this -- whilst using the row highlighting and grid drawing that NSTableView implements. The only sensible way (I can think of) is to subclass NSTableColumn to return data cells that alternate in colour and draw the grid. Or if you can persuade the default cell to draw what you want then implementing

- (void)tableView: (NSTableView *)aTableView willDisplayCell: (id)aCell forTableColumn: (NSTableColumn *)aTableColumn row: (int)aRowIndex

might be better; there is some discussion here: http://www.cocoadev.com/index.pl?AlternatingRowColors
Quote this message in a reply
honkFactory
Unregistered
 
Post: #5
OK. I reach a solution by adding the table view delegate method
Code:
- (void)tableView: (NSTableView *)aTableView willDisplayCell: (id)aCell
   forTableColumn: (NSTableColumn *)aTableColumn row: (int)aRowIndex
{
    [aCell setBackgroundColor:[NSColor colorWithCalibratedRed:0.95 green:0.95 blue: 1.0 alpha: 1.0
]];
    [aCell setDrawsBackground: YES];
}

This method is only called for cells that are going to be drawn, which is to say in rows whose indices are < the number of rows returned by the data source. Originally I had planned to give a background color to cells > this value, which the user canNOT edit. Since this method isn't called for those cell, I decided to flip this around a give a background color to cells that are drawn and therefore editable.
I should also point out that there is a bit of a problem with intercell spacing. There is a 1 pixel white border around each cell. It is not noticeable since I am using a very light background color however it would be very noticeable if you were to use a darker BGC. You can get rid of the intercell spacing by sending the table view a setIntercellSpacing: message, with the side effect that the tails of 'p','g' and 'q' will hang below the cell border. Not sure what the solution to this is.
Quote this message in a reply
honkFactory
Unregistered
 
Post: #6
I have a second, semi-related question, about table views. Lets say I have selected a row, highlighting the entire row, but I have not double clicked on a cell to edit that cell yet. Lets say I want the first key stroke to select the first cell in the row so I may start editing it. I have implemented a method that will delete an entire row when the delete key is pressed so I know how to capture the keystroke (within a table view subclass) and pass on a command to a user defined data source method. But I do not know how to force a cell to be selected from the level of the tableView or tableColumn. Any thoughts?
Quote this message in a reply
Member
Posts: 78
Joined: 2002.06
Post: #7
[self editColumn:_clickedColumn row:_clickedRow withEvent:NULL select:YES];
Quote this message in a reply
honkFactory
Unregistered
 
Post: #8
Thanks. I had seen that method in the documentation but I must have misunderstood its description. Oddly enough I have to put a
Code:
[self selectRow:[self selectedRow] byExtendingSelection:NO];

to get it to work. I was hopeful that if I passed the event that came with the keydown method (I am calling these methods from within the keydown) in the withEvent: field that it would enter key pressed into the cell automatically but no such luck. Anyone know how to do that?
Quote this message in a reply
Moderator
Posts: 771
Joined: 2003.04
Post: #9
*cough* iDevApps *cough*
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Fake NSTableView Coin 1 2,176 Dec 5, 2005 08:48 AM
Last Post: TomorrowPlusX
  NSTableView Nick 2 3,236 Aug 10, 2005 12:58 PM
Last Post: LongJumper
  Objective-C: drawing a custom class to a custom view GryphonClaw 1 4,058 Dec 10, 2004 03:32 AM
Last Post: GryphonClaw