Mimicking Finder's List View with NSTableView
Mimicking Finder's List View with NSTableView
- Subject: Mimicking Finder's List View with NSTableView
- From: John Nairn <email@hidden>
- Date: Mon, 20 Oct 2003 10:54:33 -0600
In Apple Finder's list view, the files are displayed in multiple
columns which can be customized by the user. You resize these columns
by dragging in the header while resizing the window has no affect on
column sizes. Although this list view looks a lot like an NSTableView
and I wanted the same column resizing interface in my application, it
does not appear possible to do so in NSTableView. The problem I had was
that window resizing was causing the last column to resize to fit in
the window when I did not want that to happen. Even when the table is
told that columns can not be resized, the last column still resizes
when the window resizes (sounds like an NSTableView bug to me)
Reading NSTableView documentation, there appears to be only two
resizing modes:
setAutoresizesAllColumnsToFit:
- (void) setAutoresizesAllColumnsToFit: (BOOL) flag
Controls whether the receiver proportionally resizes its columns to fit
when its superview's frame changes. If flag is YES , the difference in
width is distributed among the receiver's table columns; if flag is NO
, only the last column is resized to fit.
A flag of YES resizes all columns to fit in the window and a flag of NO
resizes the last column to fit. There is no flag to do like Apple's
Finder where window resizing has no affect on column sizes and only the
user can resize by dragging column header lines.
A Partial Solution - But the Coding is Ugly
--------------------------------------------------------
Following help from this list, I tried to Mimic Apple Finder's list
view by adding a dummy extra column. In this case the window resize
will only resize the column I do not care about. It mostly works, but
requires many special case conditions to deal with the extra column and
has one appearance problem that does not occur in Apple's Finder.
For those who are interested, a simple project is posted at
http://www.eng.utah.edu/~nairn/SimpleTable.sit
The excess coding required was:
1. Add extra column to table which has no column label and is not
editable.
2. In awake from nib call [myTable sizeLastColumnToFit], otherwise
there might be an ugly column divider in the table header rows that
could confuse users.
3. In tableView:objectValueForTableColumn:row: add special case for
excess column and return @""
4. When a column resizes (in delegate method or notification - I used
later because that was what I needed in my use of this solution) check
which column is resized and ignore resizing of the last column - here
ignore means if your application will do something with the new size
information such as save the new setting.
5. When columns are moved (in delegate method or notification - above
example uses notification), check for involvement of the last column.
If last column was dragged someplace, undo that drag. If a real column
was dragged past the last column, adjust the drag to be the real last
position or just before the extra column. The problem here is that you
can not undo this move with [myTable moveColumn:newIndex
toColumn:oldIndex] because this will post another column move
notification involving the last column resulting in an endless loop of
notifications and an eventual crash. In order to do an adjustment
column move, I had to set a flag which is checked in the next
notification. When the flag is set, the next notification resets the
flag and ignores the move. (is there a better way to block the next
notification of a certain type?)
6. The sample project does not allow column selecting or do anything
when clicking in header cells of columns. If these options are needed
for a table, more special case code would be required to handle the
extra column
Summary:
-------------
It works OK for my application although required rather ugly code to
accomplish something I thought should be easy. Perhaps it is the only
solution unless Apple would like to share how they made the Finder's
list view work in a mode not supported in NSTableView? The only ugly
interface issue is that when the last column is visible and the window
is enlarged, a column divider in the header will appear momentarily at
the old size of the last column until the window is redrawn and the
last column automatically sized to fit by NSTableView. This same ugly
interface issue does not occur in Apple's Finder.
------------
John Nairn (1-801-581-3413, FAX: 1-801-581-4816)
Web page:
http://www.eng.utah.edu/~nairn
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.