Skip navigation

Dynamic row heights for NSTableView

1594 Views 5 Replies Latest reply: Aug 18, 2012 11:08 AM by etresoft RSS
tereno10 Level 1 Level 1 (0 points)
Currently Being Moderated
Aug 17, 2012 7:12 AM

Hi everyone,


I just began writing OSX apps not too long ago. I'm having trouble with dynamically setting my row heights for an NSTableView. I've seen a really old solution where the subclass the view but I also found a solution posted by Corbin on Stack Overflow. However, I don't quite understand and am hoping someone has sample code?


For my usecase, I have a list of text of varying lengths and are displayed in a multi-line text field. The rows for the table cut off the text and so I'm trying to resize them to fit the text. When I check on the setRowSizeStyle to custom - it defaults to 71 and is fixed at 71. I hope someone can help me out. I think the solution posted by Corbin should solve my problem but I have no idea how to implement it.



  • etresoft Level 7 Level 7 (23,915 points)
    Currently Being Moderated
    Aug 17, 2012 11:40 AM (in response to tereno10)

    First of all, I suggest using the new view-based tables. That makes things much easier.


    Just define:


    - (CGFloat) tableView: (NSTableView *) tableView

      heightOfRow: (NSInteger) row;


    in your table view delegate.


    it is a little tricky to actually calculate the height you need. You can start with something like the following. Pass in the maximum size of a cell in your table.


    // Determine the ideal height for the content width.

    - (CGFloat) calculateIdealHeightForSize: (NSSize) size


      NSTextStorage * storage =

        [[NSTextStorage alloc] initWithAttributedString: self.content];


      NSTextContainer * container =

        [[NSTextContainer alloc] initWithContainerSize: size];

      NSLayoutManager * manager = [[NSLayoutManager alloc] init];


      [manager addTextContainer: container];

      [storage addLayoutManager: manager];


      [manager glyphRangeForTextContainer: container];


      NSRect idealRect = [manager usedRectForTextContainer: container];


      [manager release];

      [container release];

      [storage release];


      // Include a fudge factor.

      return idealRect.size.height + 25;


  • etresoft Level 7 Level 7 (23,915 points)
    Currently Being Moderated
    Aug 17, 2012 1:26 PM (in response to tereno10)

    You only need to call that if your row size is actively changing. If you know what it should be right away, it shouldn't be necessary.

  • etresoft Level 7 Level 7 (23,915 points)
    Currently Being Moderated
    Aug 18, 2012 11:08 AM (in response to tereno10)

    That looks like a cell-based table to me. There is nothing wrong with that, but I haven't used one of those recently.


    The problem is that you are passing the current, small size of the cell into "calculateIdealHeightForSize". It isn't going to do much with that. You have to pass the maximum size of the cell. It will attempt to layout the text in that area and return the height required. You may have to worry about borders in the cell and the layout method. I have seen suggestions that you should use the only 10.2 layout method, but I'm not sure that is true anymore.


More Like This

  • Retrieving data ...

Bookmarked By (0)


  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.