0

I'm try to expand the text view when I click the Read More Button.It works on device below iOS 11 but It cannot expand very smooth in iOS 11 version. why?

When I tapped the read more button will run this code.

tableView.beginUpdates()
let txtView = cell2.aboutTxtView!
let txtStr = cell2.aboutTxtView.text!
let btn = cell2.aboutReadBtn!

if self.aboutTag == 0{
    let height = getRowHeightFromTextView(strText: txtStr, txtView: txtView)
    cell2.aboutTxtView_HeightConstraint.constant = height
    self.view.layoutIfNeeded()
    btn.setTitle("Show Less", for: .normal)
    self.aboutTag = 1
    self.aboutHeight = 140.0 + height - 84.0
    tableView.endUpdates()
}
else
{
    cell2.aboutTxtView_HeightConstraint.constant = 84
    self.view.layoutIfNeeded()
    btn.setTitle("Read More", for: .normal)
    self.aboutTag = 0
    self.aboutHeight = 140.0
    tableView.endUpdates()
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

    if indexPath.section == 1
    {
        return aboutHeight
    }
}

Below iOS 11 Version

enter image description here

iOS 11 Version

enter image description here

What i have tried.

(1)

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

(2)

override func viewDidAppear(_ animated: Bool)
{
    super.viewDidAppear(animated)

    if #available(iOS 11, *)
    {
        self.tableView.contentInsetAdjustmentBehavior = .never
    }
}
Lester
  • 701
  • 1
  • 9
  • 47

3 Answers3

0

Don't update table view just update single cell inside table view and set height for cell.

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

    if indexPath.section == 1
    {
        return aboutHeight
    }
    return UITableViewAutomaticDimension
}

Then Update only About Me cell. It will only update single cell so cell expansion will be smooth.

// Add your code to update cell height 
self.tableView.beginUpdates()
let indexPath = IndexPath(row: 0, section: 1)
self.tableView.reloadRows(at: [indexPath], with: UITableViewRowAnimation.automatic)
self.tableView.endUpdates()

Update :

self.tableView.beginUpdates()
let indexPath = IndexPath(row: 0, section: 1)

let txtView = cell2.aboutTxtView!
let txtStr = cell2.aboutTxtView.text!
let btn = cell2.aboutReadBtn!

if self.aboutTag == 0{
    let height = getRowHeightFromTextView(strText: txtStr, txtView: txtView)
    cell2.aboutTxtView_HeightConstraint.constant = height
    btn.setTitle("Show Less", for: .normal)
    self.aboutTag = 1
    self.aboutHeight = 140.0 + height - 84.0
}
else
{
    cell2.aboutTxtView_HeightConstraint.constant = 84
    btn.setTitle("Read More", for: .normal)
    self.aboutTag = 0
    self.aboutHeight = 140.0
}
tableView.endUpdates()

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

    if indexPath.section == 1
    {
        return aboutHeight
    }
    return UITableViewAutomaticDimension
}
Sid Mhatre
  • 3,272
  • 1
  • 19
  • 38
  • Try removing `estimatedHeightForRowAt`. – Sid Mhatre Mar 02 '18 at 12:00
  • Check out Update and use same code and remove `self.view.layoutIfNeeded()` and What i have tried 1 and 2. – Sid Mhatre Mar 02 '18 at 12:19
  • I have removed the code in "what i have". and the constraint height of the textview will not update if I remove `self.view.layoutIfNeeded()` , and also the 'laggy' or 'scroll to top' effect still at there when i tapped the button – Lester Mar 02 '18 at 12:31
  • `self.view.layoutIfNeeded()` Not require for expanding cell in Uitableview . If you change in height and content of cell then it will updated. – Sid Mhatre Mar 02 '18 at 12:38
0

If @Sid's answer doesn't solve your problem.

You should call layoutIfNeeded, self.view.layoutIfNeeded() after any updates in the UI. Also, use the main queue for it.

DispatchQueue.main.async {
   self.view.layoutIfNeeded()
}

It will update the views frames.

Naresh
  • 334
  • 1
  • 4
  • 17
0

I fixed this by adding this three code.

self.tableView.estimatedRowHeight = 0;
self.tableView.estimatedSectionHeaderHeight = 0;
self.tableView.estimatedSectionFooterHeight = 0;

Source: https://stackoverflow.com/a/46350318/8393564
Source: https://stackoverflow.com/a/46257601/8393564

Lester
  • 701
  • 1
  • 9
  • 47