无法在 iOS 11 Swift 中顺利展开文本视图

Cannot expand text view smoothly in iOS 11 Swift

我尝试在单击“阅读更多”时扩展文本视图 Button.It 适用于 iOS 11 以下的设备,但在 iOS 11 版本中无法非常流畅地扩展。为什么?

当我点击阅读更多按钮时将 运行 此代码。

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
    }
}

低于iOS11版本

iOS 11 版本

我试过的。

(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
    }
}

不更新 table 视图只更新 table 视图中的单个单元格并设置单元格的高度。

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

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

然后只更新 关于我 单元格。它只会更新单个单元格,因此单元格扩展会很顺利。

// 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()

更新:

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 的回答没有解决您的问题。

您应该在 UI 中的任何更新后调用 layoutIfNeeded,self.view.layoutIfNeeded()。另外,使用主队列。

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

它将更新视图框架。

我通过添加这三个代码解决了这个问题。

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

来源:
资料来源: