2

For this i studied many tutorial But I was not able to find solution for my problem

I want to show textView's on table cell. and Cell should expand with the text entered by user in the textView.

As I found there are two ways to do this

first one:-

self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 30;


func textViewDidChange(_ textView: UITextView!, for indexPath: IndexPath!) {
    self.presenter?.valueChanged(section: indexPath.section, value: textView.text)
    isChanged = true

    self.tableView.beginUpdates()
    self.tableView.endUpdates()
}
Make the table view to adjust height automatically. But in this case when there is no data to display in cellForRow at index path then textview height is automatically adjusted to 0 and i was not able to select the text view.

Second way :-

remove the automatic dimension code

// MARK: TextViewTableViewCellDelegate
func textViewDidChange(_ textView: UITextView!, for indexPath: IndexPath!) {
    self.presenter?.valueChanged(section: indexPath.section, value: textView.text)
    isChanged = true

    self.tableView.beginUpdates()
    self.tableView.endUpdates()
}

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    var ret = DEFAULT_CELL_HEIGHT

    if let cell = tableView.dequeueReusableCell(withIdentifier: SECTION_TYPE_FREEFORM) as? TextViewTableViewCell {
        cell.textView.text = self.presenter?.cellBody(section: indexPath.section)
        cell.textView.sizeToFit()

        let width = self.view.frame.size.width - HORIZONTAL_PADDING
        let calculatedHeight = cell.textView.sizeThatFits(CGSize(width: width, height: CGFloat(MAXFLOAT))).height + 10
        if(calculatedHeight > ret) {
            ret = calculatedHeight
        }
    }

    return ret
}

But in this I checked that then I create cell in the heightForRowAtIndexPath method then textview height is not appropriate. And it is not exactly working to adjust the height automatically.

Kindly suggest the solution for the same. Thanks in Advance.

unknown
  • 23
  • 6
Parv Bhasker
  • 1,773
  • 1
  • 20
  • 39
  • 1
    use autolayout ,func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat{ return UITableViewAutomaticDimension } – Vasanthan Prem Apr 19 '18 at 05:24
  • For this i used height constraint of size 48 with "greater than or equal" option. In this case for the first time text view shows with height 48 but not expanding with text. I just put a text view on the table cell with top, bottom, leading and trailing constraints. – Parv Bhasker Apr 19 '18 at 05:27
  • @VasanthanPrem As soon as i remove the height constraint it just make the textview height 0. I removed the height constraints and used the above two methods and textview height is showing 0 – Parv Bhasker Apr 19 '18 at 05:41
  • if textview is only for reading purpose use lable – Vasanthan Prem Apr 19 '18 at 05:41
  • no if i have already data then i have to show it in the start and let user edit or add data in the texview – Parv Bhasker Apr 19 '18 at 05:44

2 Answers2

3

What about adding a minimum height constraint to your textview and using the first method? Just make sure the scrolling of your UITextView is disabled. Because the only issue you have on the first solution is the cell height being too small when nothing is in there...

Otherwise you code seems pretty good and might give you smooth animation.

RomOne
  • 2,065
  • 17
  • 29
  • For this i used height constraint of size 48 with "greater than or equal" option. In this case for the first time text view shows with height 48 but not expanding with text. I just put a text view on the table cell with top, bottom, leading and trailing constraints. – Parv Bhasker Apr 19 '18 at 05:27
  • when i put the height constraint of size 48 with "greater than or equal" option then I don't know why cell is not expanding with entered text. – Parv Bhasker Apr 19 '18 at 05:32
  • Maybe because at the beginning it doesn't have to. Have you try to keep going, and add more text. Also you should disable the scrolling in your textview – RomOne Apr 19 '18 at 05:34
  • yes after disabling the scroll of textview it start working. Thanks – Parv Bhasker Apr 19 '18 at 06:02
3
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  return UITableViewAutomaticDimension 
}

func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat { 
  return yourinitialheight 
}

remove height constraint, and set top, bottom, leading and trailing constraints.

Jayesh Thanki
  • 2,037
  • 2
  • 23
  • 32