How to adapt a cell's height to its contents

Hello,

In a new project, I have an array of text cells in an editable tableView. I’d like to:

a) allow multi-line text entry
b) set the height of the cell to adapt to its content.

Precisions: I’m working with raw (not rich) text, so not to worry about size or font changes (each cell will be stored in a plist file).
Each line of the table view is a AS record/NSDictionary containing three entries : ID (integer), text and textHeight (integer)

So far I tried:

  • set the line’s height within the controlTextDidChange_ method – ends with a bang:

bind the line height (in IB) to the controller’s selection textHeight. Works but all the lines are set to the same height.

bind the line height (in IB) to the controller’s arrangedObjects textHeight. Bang again.

Any ideas? Maybe with a view-based tableView? Thanks!

Hi,

normally you can specify different heights for each row by implementing the delegate method


- (CGFloat)tableView:(NSTableView *)tableView heightOfRow:(NSInteger)row

Consider this note (from the documentation)
Although table views may cache the returned values, you should ensure that this method is efficient. When you change a row’s height you must invalidate the existing row height by calling noteHeightOfRowsWithIndexesChanged:. NSTableView automatically invalidates its entire row height cache when reloadData and noteNumberOfRowsChanged are called.