Sizing NSTableView to data

  • I am working on the ODBCkit's Query Tool to make it work across a wide variety of data sources - so far all the major commercial DB's, Firebird, MySQL and even SQLite! Got an ODBC target and test DB you'd like to test? Throw it my way!

    One problem that's come up in a couple of different places is that many SQL data types are fixed-width. I ask the recordset for the width, but it lies - it says 255 chars, but in fact there's only maybe 25 characters actually in the field. It is also surprisingly common to find numbers held in huge fixed-char fields. When I space out my TableView based on what the DB tells me, I end up with lots of whitespace.

    I notice that *after* the DataSource is done setting up and the data is in the display, double-clicking the "divider line" in the header gives me perfect results. Is there a way to simulate that double-click in code? Ideally I'd like to find out what the width would be if I did the action, then adjust it for max/min sizes in the code.

    Thanks!

    p.s. First Cocoa project in years. Holy smokes XCode got a lot better since I last looked. That and this SSD rocks.
  • On Thu, Jun 28, 2012, at 04:42 PM, Maury Markowitz wrote:
    > I am working on the ODBCkit's Query Tool to make it work across a wide
    > variety of data sources - so far all the major commercial DB's, Firebird,
    > MySQL and even SQLite! Got an ODBC target and test DB you'd like to test?
    > Throw it my way!
    >
    > One problem that's come up in a couple of different places is that many
    > SQL data types are fixed-width. I ask the recordset for the width, but it
    > lies - it says 255 chars, but in fact there's only maybe 25 characters
    > actually in the field. It is also surprisingly common to find numbers
    > held in huge fixed-char fields. When I space out my TableView based on
    > what the DB tells me, I end up with lots of whitespace.
    >
    > I notice that *after* the DataSource is done setting up and the data is
    > in the display, double-clicking the "divider line" in the header gives me
    > perfect results. Is there a way to simulate that double-click in code?
    > Ideally I'd like to find out what the width would be if I did the action,
    > then adjust it for max/min sizes in the code.

    According to the AppKit release notes [1], NSTableView sizes a column to
    fit by taking the maximum of the sizes of the preparedCellAtRow:column:
    for some number of rows. (I believe Corbin has said it takes a random
    subset when the number of rows in the data source is large). This is
    presumably why they recommend you implement
    -tableView:sizeToFitWidthOfColumn: yourself rather than have NSTableView
    do it.

    I'd say you should file an enhancement request asking for the ability to
    ask NSTableView to size a particular column to fit, just as if the user
    had double-clicked the divider in the header, as it only seems to have
    methods that distribute remaining size uniformly or only to the last
    column.

    In the meantime, implement -tableView:sizeToFitWidthOfColumn: to return
    the right value for each column, and then just call -sizeToFit on the
    table view.

    [1]
    https://developer.apple.com/library/mac/#releasenotes/Cocoa/AppKitOlderNote
    s.html


    --Kyle Sluder
  • Unfortunately, the sizeToFit method seems to create and infinite loop condition.  trying to sort out WHY it happens.

    On Jul 18, 2012, at 1:13 AM, Kyle Sluder wrote:

    > On Thu, Jun 28, 2012, at 04:42 PM, Maury Markowitz wrote:
    >> I am working on the ODBCkit's Query Tool to make it work across a wide
    >> variety of data sources - so far all the major commercial DB's, Firebird,
    >> MySQL and even SQLite! Got an ODBC target and test DB you'd like to test?
    >> Throw it my way!
    >>
    >> One problem that's come up in a couple of different places is that many
    >> SQL data types are fixed-width. I ask the recordset for the width, but it
    >> lies - it says 255 chars, but in fact there's only maybe 25 characters
    >> actually in the field. It is also surprisingly common to find numbers
    >> held in huge fixed-char fields. When I space out my TableView based on
    >> what the DB tells me, I end up with lots of whitespace.
    >>
    >> I notice that *after* the DataSource is done setting up and the data is
    >> in the display, double-clicking the "divider line" in the header gives me
    >> perfect results. Is there a way to simulate that double-click in code?
    >> Ideally I'd like to find out what the width would be if I did the action,
    >> then adjust it for max/min sizes in the code.
    >
    > According to the AppKit release notes [1], NSTableView sizes a column to
    > fit by taking the maximum of the sizes of the preparedCellAtRow:column:
    > for some number of rows. (I believe Corbin has said it takes a random
    > subset when the number of rows in the data source is large). This is
    > presumably why they recommend you implement
    > -tableView:sizeToFitWidthOfColumn: yourself rather than have NSTableView
    > do it.
    >
    > I'd say you should file an enhancement request asking for the ability to
    > ask NSTableView to size a particular column to fit, just as if the user
    > had double-clicked the divider in the header, as it only seems to have
    > methods that distribute remaining size uniformly or only to the last
    > column.
    >
    > In the meantime, implement -tableView:sizeToFitWidthOfColumn: to return
    > the right value for each column, and then just call -sizeToFit on the
    > table view.
    >
    > [1]
    > https://developer.apple.com/library/mac/#releasenotes/Cocoa/AppKitOlderNote
    s.html

    >
    > --Kyle Sluder
previous month june 2012 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  
Go to today