Dynamic row height for NSTableView in Swift/Cocoa -


edit: question not answered linked reply. i'm still stuck on this...

i've got table use chat messages. each message variably-sized box including text. want row height in table dynamically change depending on size of message box.

if tick 'automatic' size style in ib, makes of rows have tiny heights. looks ios have uitableviewautomaticdimension automatically scales table, can't find nstableview.

i know there's option create following function:

func tableview(_ tableview: nstableview, heightofrow row: int) -> cgfloat {        return 50 // ???? } 

and able set row height using (the above sets of rows height 50), don't know how make dependent on size of text in box. how can efficiently?

here's 1 solution

first set nstextfield in code. "virtual" field in never drawn

    var fakefield = nstextfield()   // set fontsize etc use in table 

then in heightofrow function

func tableview(tableview: nstableview, heightofrow row: int) -> cgfloat {      let item = yourarray[row]      let fakefield.stringvalue = item      // how text out of data array depends on how set      let yourheight = fakefield.cell!.cellsizeforbounds(nsmakerect(cgfloat(0.0), cgfloat(0.0), yourwidth, cgfloat(flt_max))).height     // yourwidth = width of cell cgfloat.        return yourheight } 

more details , nstextfield extensions can found here. note answer in swift 2.3 (haven't made plunge 3 yet)


Comments

Popular posts from this blog

unity3d - Rotate an object to face an opposite direction -

angular - Is it possible to get native element for formControl? -

javascript - Why jQuery Select box change event is now working? -