• <tfoot id='LDApm'></tfoot>
      <bdo id='LDApm'></bdo><ul id='LDApm'></ul>
  • <small id='LDApm'></small><noframes id='LDApm'>

    <legend id='LDApm'><style id='LDApm'><dir id='LDApm'><q id='LDApm'></q></dir></style></legend>

      1. <i id='LDApm'><tr id='LDApm'><dt id='LDApm'><q id='LDApm'><span id='LDApm'><b id='LDApm'><form id='LDApm'><ins id='LDApm'></ins><ul id='LDApm'></ul><sub id='LDApm'></sub></form><legend id='LDApm'></legend><bdo id='LDApm'><pre id='LDApm'><center id='LDApm'></center></pre></bdo></b><th id='LDApm'></th></span></q></dt></tr></i><div id='LDApm'><tfoot id='LDApm'></tfoot><dl id='LDApm'><fieldset id='LDApm'></fieldset></dl></div>

        键入时调整包含 UITextView 的 UITableViewCell 的大小

        时间:2023-09-10

          <small id='Gqwbr'></small><noframes id='Gqwbr'>

            • <i id='Gqwbr'><tr id='Gqwbr'><dt id='Gqwbr'><q id='Gqwbr'><span id='Gqwbr'><b id='Gqwbr'><form id='Gqwbr'><ins id='Gqwbr'></ins><ul id='Gqwbr'></ul><sub id='Gqwbr'></sub></form><legend id='Gqwbr'></legend><bdo id='Gqwbr'><pre id='Gqwbr'><center id='Gqwbr'></center></pre></bdo></b><th id='Gqwbr'></th></span></q></dt></tr></i><div id='Gqwbr'><tfoot id='Gqwbr'></tfoot><dl id='Gqwbr'><fieldset id='Gqwbr'></fieldset></dl></div>

              <tfoot id='Gqwbr'></tfoot>

              <legend id='Gqwbr'><style id='Gqwbr'><dir id='Gqwbr'><q id='Gqwbr'></q></dir></style></legend>
                • <bdo id='Gqwbr'></bdo><ul id='Gqwbr'></ul>
                    <tbody id='Gqwbr'></tbody>
                  本文介绍了键入时调整包含 UITextView 的 UITableViewCell 的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个包含自定义单元格的 UITableViewController.每个单元格都是使用 nib 创建的,并包含一个不可滚动的 UITextView.我在每个单元格内添加了约束,以便单元格使其高度适应 UITextView 的内容.所以最初我的控制器看起来像这样:

                  现在我希望当用户在单元格中键入内容时,其内容会自动适应.这个问题已经被问过很多次了,特别看

                  如果我向下和向上滚动 tableView 以强制调用 cellForRowAtIndexPath,我会恢复单元格的正确高度:

                  请注意,我确实没有实现 heightForRowAtIndexPath,因为我希望 autoLayout 能够解决这个问题.

                  有人可以告诉我我做错了什么或在这里帮助我吗?非常感谢!

                  解决方案

                  这是一个对我来说很好的快速解决方案.如果您使用自动布局,则需要为estimatedRowHeight 分配一个值,然后返回UITableViewAutomaticDimension 作为行高.最后在文本视图委托中执行类似于下面的操作.

                  覆盖 func viewDidLoad() {super.viewDidLoad()self.tableView.estimatedRowHeight = 44.0}覆盖 func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) ->CGF浮动{返回 UITableViewAutomaticDimension}//MARK: UITextViewDelegatefunc textViewDidChange(textView: UITextView) {//计算文本视图是否会改变高度,然后只强制//如果更新,则要更新的表.同时禁用动画//防止jankness".让 startHeight = textView.frame.size.heightlet calcHeight = textView.sizeThatFits(textView.frame.size).height//仅限iOS 8+如果 startHeight != calcHeight {UIView.setAnimationsEnabled(false)//禁用动画self.tableView.beginUpdates()self.tableView.endUpdates()//如果滚动,可能需要在此处插入其他内容//以一种意想不到的方式表.这滚动到底部//表的.(虽然你可能需要更多的东西//如果在中间编辑会很复杂.)让 scrollTo = self.tableView.contentSize.height - self.tableView.frame.size.heightself.tableView.setContentOffset(CGPoint(x: 0, y: scrollTo), 动画: false)UIView.setAnimationsEnabled(true)//重新启用动画.}

                  I have an UITableViewController that contains a custom cell. Each cell was created using a nib and contains a single non-scrollable UITextView. I have added constraints inside each cell so that the cell adapts its height to the content of the UITextView. So initially my controller looks like this :

                  Now I want that when the user types something in a cell its content automatically adapts. This question has been asked many times, see in particular this or the second answer here. I have thus written the following delegate in my code :

                  - (BOOL) textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString*)text {
                      [self.tableView beginUpdates];
                      [self.tableView endUpdates];
                      return YES;
                  }
                  

                  However it leads to the following strange behavior : all constraints are ignored and all cells height collapse to the minimal value. See the picture below:

                  If I scroll down and up the tableView in order to force for a new call of cellForRowAtIndexPath, I recover the correct heights for the cells:

                  Note that I did not implement heightForRowAtIndexPath as I expect autoLayout to take care of this.

                  Could someone tell me what I did wrong or help me out here ? Thank you very much !

                  解决方案

                  Here is a swift solution that is working fine for me. Provided you are using auto layout, you need assign a value to estimatedRowHeight and then return UITableViewAutomaticDimension for the row height. Finally do something similar to below in the text view delegate.

                  override func viewDidLoad() {
                      super.viewDidLoad()
                      self.tableView.estimatedRowHeight = 44.0
                  }
                  
                  override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
                      return UITableViewAutomaticDimension
                  }
                  
                  // MARK: UITextViewDelegate
                  func textViewDidChange(textView: UITextView) {
                  
                      // Calculate if the text view will change height, then only force 
                      // the table to update if it does.  Also disable animations to
                      // prevent "jankiness".
                  
                      let startHeight = textView.frame.size.height
                      let calcHeight = textView.sizeThatFits(textView.frame.size).height  //iOS 8+ only
                  
                      if startHeight != calcHeight {
                  
                          UIView.setAnimationsEnabled(false) // Disable animations
                          self.tableView.beginUpdates()
                          self.tableView.endUpdates()
                  
                          // Might need to insert additional stuff here if scrolls
                          // table in an unexpected way.  This scrolls to the bottom
                          // of the table. (Though you might need something more
                          // complicated if editing in the middle.)
                  
                          let scrollTo = self.tableView.contentSize.height - self.tableView.frame.size.height
                          self.tableView.setContentOffset(CGPoint(x: 0, y: scrollTo), animated: false)
                  
                          UIView.setAnimationsEnabled(true)  // Re-enable animations.
                  }
                  

                  这篇关于键入时调整包含 UITextView 的 UITableViewCell 的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在 UILabel 中垂直对齐文本(注意:使用 AutoLayout) 下一篇:使用自动布局自定义纵横比

                  相关文章

                • <i id='oDfbC'><tr id='oDfbC'><dt id='oDfbC'><q id='oDfbC'><span id='oDfbC'><b id='oDfbC'><form id='oDfbC'><ins id='oDfbC'></ins><ul id='oDfbC'></ul><sub id='oDfbC'></sub></form><legend id='oDfbC'></legend><bdo id='oDfbC'><pre id='oDfbC'><center id='oDfbC'></center></pre></bdo></b><th id='oDfbC'></th></span></q></dt></tr></i><div id='oDfbC'><tfoot id='oDfbC'></tfoot><dl id='oDfbC'><fieldset id='oDfbC'></fieldset></dl></div>
                    <bdo id='oDfbC'></bdo><ul id='oDfbC'></ul>

                    <legend id='oDfbC'><style id='oDfbC'><dir id='oDfbC'><q id='oDfbC'></q></dir></style></legend><tfoot id='oDfbC'></tfoot>
                  1. <small id='oDfbC'></small><noframes id='oDfbC'>