ios - UITableViewCell with multline input field ? -


i want create uitableview cell , multiline input field (textview). height of textview or cell should increase automatically user enter text.

hi there: has been bugging me day i've put think has cracked it. i'm assuming know how put basic tableview together.

you need add uitextview each row, and, crucially, make tableviewcontroller delegate each uitextview. alongside array of string values each of rows, need have array of heights (cgfloats) each row. crucial aspect assign each textview numerical tag equal indexpath.row cell allows keep track of textview dealing with. start out array of identical values, idea update array needed when text gets big existing frame, demonstrate below.

i implemented following 2 delegate functions bespoke function update layout required:

func updatelayout(fortextview: uitextview) -> void {     guard fortextview.tag < self.data.count else { return } // make sure we're not going try , access value not exist      self.data[fortextview.tag] = fortextview.text // update our data array reflect new string value cell's text view      let newheight : cgfloat = fortextview.frame.height // new cell height based on height of text view     let oldheight : cgfloat = self.tableview.cellforrowatindexpath(nsindexpath(forrow: fortextview.tag, insection: 0))!.frame.height // retrieving existing height of cell      self.heights[fortextview.tag] = newheight + 2 // set new value in heights array 1 point buffer     // animate ease transition     uiview.animatewithduration(0.25) {          cell in self.tableview.visiblecells {             if let indexpath = self.tableview.indexpathforcell(cell) {                 // need update edited cell , cells below                 if indexpath.row > fortextview.tag {                     cell.frame = cgrect(origin: cgpointmake(cell.frame.origin.x, cell.frame.origin.y + fortextview.frame.height + 2 - oldheight), size: cgsizemake(cell.frame.width, self.heights[indexpath.row]))                 }                 else if indexpath.row == fortextview.tag                 {                     cell.frame = cgrect(origin: cell.frame.origin, size: cgsizemake(cell.frame.width, self.heights[indexpath.row]))                 }             }         }     } }  func textviewdidendediting(textview: uitextview) {     // update cell , exit     self.updatelayout(textview) }  func textview(textview: uitextview, shouldchangetextinrange range: nsrange, replacementtext text: string) -> bool {     // more complicated: need update frame maintain focus in textview user has not finished editing yet     var textframe : cgrect = textview.frame     let newheight : cgfloat = ceil(textview.contentsize.height)     if newheight - textframe.height > 0 {         textframe.size.height = ceil(textview.contentsize.height)         textview.frame = textframe         textview.setcontentoffset(cgpointmake(0, 5), animated: false)         self.updatelayout(textview)     }      // code puts active cursor @ end of textview's text     textview.selectedrange = nsrange(location: textview.text.characters.count, length: 0)     textview.becomefirstresponder()     return true } 

the tricky part getting tableview's layout update while keeping cursor's focus in field being actively edited.

i pleased result gives smooth transition when text field needs expand.

i'd rather not send full code it's better put these things yourself; however, if stuck drop me line , i'll send over. best!


Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -