ios - UITextView and cell dynamically update height based on content of textview? -
i have been having issue regarding uitextview inside custom cell. textview works besides issue. have uitextviewdelegate methods setup when happens textview, have methods connected.
i know how can have custom cell dynamically increase it's height user types. once user hits end of line of textview, have textview add line automatically. have been searching online while, , of examples issue in objective c. not swift code. appreciate help.
i have cell hooked in tableview file such..
func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell { let cell: cellcustom = the_tableview.dequeuereusablecellwithidentifier("cell") as! cellcustom return cell }
in custom cell file, have delegates set up...
func textviewdidchange(textview: uitextview) { } func textview(textview: uitextview, shouldchangetextinrange range: nsrange, replacementtext text: string) -> bool { return true }
i have tried things ...
self.textview.sizetofit() self.the_tableview.rowheight = uitableviewautomaticdimension self.the_tableview.estimatedrowheight = 47
when did that, textview took text, did not increase in size until resigned it's first responder , scrolled cell gone. when cell bounces down, updates.
i have added constraints , project has 0 risks or errors , works perfectly.
to clarify, uitextview change height based on content user types while typing it, meaning custom cell , tableview have update. think of clear app on iphone. it's that. cell updates dynamically user types, increasing in height, while adding new line once user reaches end. i'm reaching for.
though not new developing means, never got point in development before. extremely appreciated. since have custom cell in file, have difficulty accessing cell elements tableview file. still novice guess. reading this.
this works ios 7 too
this goes tableviewcell
protocol heightfortextview { func heightoftextview(height: cgfloat) } class tableviewcell: uitableviewcell { @iboutlet weak var textview: uitextview! var delgate:heightfortextview? override func awakefromnib() { super.awakefromnib() // initialization code } func textviewdidchange(textview: uitextview) { var fixedwidth: cgfloat = textview.frame.size.width var newsize: cgsize = textview.sizethatfits(cgsizemake(fixedwidth, cgfloat.max)) if let iudelegate = self.delgate { iudelegate.heightoftextview(newsize.height) } } }
your tableview controller should be
class tableviewcontroller: uitableviewcontroller,heightfortextview { var textviewheight = cgfloat() //in cell row method set your delegate override func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell { let cell = tableview.dequeuereusablecellwithidentifier("cell", forindexpath: indexpath) as! tableviewcell cell.delgate = self return cell } //delegate method func heightoftextview(height: cgfloat) { textviewheight = height self.tableview.beginupdates() self.tableview.endupdates() } override func tableview(tableview: uitableview, heightforrowatindexpath indexpath: nsindexpath) -> cgfloat { return textviewheight + 70 } }
Comments
Post a Comment