NSTableView...how do the "real" programmers do this?

Discussion in 'Mac Programming' started by mdeh, Mar 22, 2009.

  1. macrumors 6502

    Joined:
    Jan 3, 2009
    #1
    Hi All,
    I wonder if I could get some feedback for this particular problem I have run into using NSTableview. (forever pushing further into Hillegas!!)

    My goal was to simply set up a table of an arbitrary number of columns. (No problem here, used IB etc). ( Lets just say it was 11).

    My problem was how to get the columns to be of equal width. After some reading ( well...a lot of reading!! :) ) and some asking on cocoa, the way I came up with was to, in ( the awakeFromNib method) iterate through all the colums setting the maxWidth property of each column.

    This works well, unless I minimize the tableview and then, the columns no longer remain equal in width to each other, but the first 2 get disproportionately wider than the rest.


    So, my questions are this.

    I had expected to set this up in IB, setting some attributes of NSTableView, not expecting to have to do this amount ( admittedly little) coding. Is this the way **real** programmers do it?

    From what I can see, each column expects **some** value, as left to itself, some of my columns set their max values to: "(340282306073709652508363335590014353408.000000)"

    Lastly, is there an obvious reason as to why the columns get distorted when the entire tableview is "squeezed", for want of a better description.

    Thank you as always....
     
  2. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    You could just set the column autoresizing behavior, which can be done in IB or in code (see NSTableViewColumnAutoresizingStyle):
    colsizing.png

    Looks like it might be some form of FLT_MAX... I think IB does this since I've seen it before too.

    Can you post a screenshot of an example?
     
  3. thread starter macrumors 6502

    Joined:
    Jan 3, 2009
    #3


    Well, I think I solved this one. NSTableColumn's width is declared as CGFloat type. The maxWidth is defined as

    Sure! no problem!

    Here is the setup. An NSTableView. One Class ( AppController) whose outlet is set to the tableview.
    In the method awakeFromNib this code, which is variously commented out depending upon what is being tried.


    Code:
    
    #define  COLMAXWIDTH 1000
    -(void) awakeFromNib
    {
    	NSArray *a = [ tv tableColumns];
    	/* set each column */
    	for ( NSTableColumn *tc in a)
    		[tc setMaxWidth: COLMAXWIDTH]; 
    	/* declare the sizes of the columns */
    	
    	for ( NSTableColumn *tc in a)
    		NSLog(@"Column size: %f", [tc maxWidth]);
    }
    So, the first experiment is to simply drag in an NSTableView and set the columns to 10.

    Here is the screenshot. (cropped to save space)

    small view of table.png

    with these attributes:

    Attributes.png

    And this the console.

    If I set the column widths, say to 750 Max Width, the console shows this ( I wont' show it ...but it does) and the screen shot is ( first expanded..the last columns do show uniform width)
    750_maxWidth_expanded.png

    and *compressed*
    750_compressed.png

    So, not sure what to make of this...I was able to get it to be uniform once, but now cannot get back to that, but the same issue happened. As the window was made narrower, at some point the first 1 or 2 columns ( cannot now recall) stopped becoming narrower and the uniform width was compromised.

    Thanks for helping.
     
  4. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #4
    If you click a column in IB you can prevent it from resizing. You also may want to set the minWidth as well.

    If you want equal widths all the time, you need to first set it up in IB like this, and then set Uniform.

    I'd also suggest reading the different NSTableViewColumnAutoresizingStyle values in the docs so you know what those are doing.
     
  5. thread starter macrumors 6502

    Joined:
    Jan 3, 2009
    #5
    thanks kainjow....
    As I am beginning to suspect, NSTableView is quite a complicated beast! Somehow, just imagined a few clicks here and there in IB and all would be done.
    Thanks again for taking the time to give your feedback...much appreciated.
     

Share This Page