【问题标题】:Cannot expand text view smoothly in iOS 11 Swift无法在 iOS 11 Swift 中顺利展开文本视图
【发布时间】:2018-08-10 07:22:44
【问题描述】:

当我点击阅读更多按钮时,我尝试展开文本视图。它适用于 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
    }
}

iOS 11 以下版本

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

【问题讨论】:

    标签: swift textview


    【解决方案1】:

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

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

    来源:https://stackoverflow.com/a/46350318/8393564
    来源:https://stackoverflow.com/a/46257601/8393564

    【讨论】:

      【解决方案2】:

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

      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
      }
      

      【讨论】:

      • 尝试删除estimatedHeightForRowAt
      • 检查更新并使用相同的代码并删除 self.view.layoutIfNeeded() 和我尝试过的 1 和 2。
      • 我已经删除了“我所拥有的”中的代码。如果我删除 self.view.layoutIfNeeded() ,文本视图的约束高度将不会更新,当我点击按钮时,“滞后”或“滚动到顶部”效果仍然存在
      • self.view.layoutIfNeeded() 在 Uitableview 中扩展单元格不需要。如果您更改单元格的高度和内容,那么它将更新。
      【解决方案3】:

      如果@Sid 的回答不能解决您的问题。

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

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

      它将更新视图框架。

      【讨论】:

      • 我也试过self.tableView.layoutIfNeeded()cell2.layoutIfNeeded()
      • 是的,cell2.aboutTxtView_HeightConstraint.constant,这是高度约束
      猜你喜欢
      • 2012-10-26
      • 1970-01-01
      • 2015-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多