【问题标题】:Expand UITextView height (read more effect) in UITableViewCell在 UITableViewCell 中展开 UITextView 高度(阅读更多效果)
【发布时间】:2017-10-12 07:43:43
【问题描述】:

我正在尝试使用动画扩展 UITextView,当用户点击下方的阅读更多按钮时。

我尝试了很多方法并提出了几乎可行的解决方案:

func readMore() {
    self.tableView.beginUpdates()
    cell.descTextViewHeightConstraint.constant = cell.descTextView.expectedHeight()
    self.tableView.endUpdates()
}

UItextView+高度

extension UITextView {
    func expectedHeight() -> CGFloat {
        let textView: UITextView = UITextView(frame: CGRect(x: 0, y: 0, width: self.frame.size.width, height: CGFloat.greatestFiniteMagnitude))
        textView.font = self.font
        textView.text = self.text
        textView.sizeToFit()
        return textView.frame.height
    }
}

此解决方案有效,但有一个副作用 - 当动画结束时,UITableViewCell 中的一些元素被隐藏(标题标签)。

我也尝试过在动画结束时调用 tableView.reloadData(),这很有效,但有时也会有副作用。

【问题讨论】:

  • 这个问题几乎每天都会被问到,这就是为什么我创建了这个示例项目github.com/bavarskis/ExpandingTableViewCell.git 看看它,很简单。
  • 我在我的代码中发现了一个错误,这导致了我提到的副作用之一(点击时阅读更多按钮没有隐藏)。我在 cellForRow 函数中以编程方式创建了阅读更多按钮。问题是,当我多次滚动到该单元格时,每次都会创建按钮,并且只有最后一个创建的按钮被隐藏。
  • @AuRis OP 询问的“阅读更多”按钮/效果在哪里?
  • @AwaisFayyaz 示例项目展示了如何在输入UITextView 时更新单元格高度。基于示例项目,您可以轻松实现 Read More 功能。该项目不是这个特定问题的答案。
  • @filip.karas 你能分享你阅读更多的 textview 代码吗?

标签: ios swift xcode uitableview animation


【解决方案1】:

为什么不“玩”一下 numberOfLines 属性?

对于一个项目,我做了类似的事情(还有一个 UIImage 箭头可以旋转......)。一切都在一个自定义单元类中:

func setExpanded(_ isExpanded: Bool) {
    if isExpanded {
        lblFullDescription.numberOfLines = 0
        UIView.animate(withDuration: Constants.NavigationBar.AnimationTime, animations: { [weak self] in
            self?.imgArrow.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi))
        })

    } else {
        lblFullDescription.numberOfLines = 2
        UIView.animate(withDuration: Constants.NavigationBar.AnimationTime, animations: { [weak self] in
            self?.imgArrow.transform = CGAffineTransform(rotationAngle: CGFloat(0))
        })
    }
}

并将其添加到包含 UITableView 的 ViewController 中:

tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 42

【讨论】:

  • 我已经尝试过这种方法。它正在工作,但问题是它“从底部”动画。当我将行数设置为 2 时,我可以看到内容的前 2 行,这很好。但是当我将行数设置为 0 时,我只能看到标签中内容的最后 2 行(不是前 2 行),并且在动画过程中,内容被绑定到底部,所以我是第一行最后显示。
【解决方案2】:

您可以对单元格进行回调并更新其约束

func updateTableViewUIfor(indexPath: IndexPath) {
    self.tableView.beginUpdates()
    theCell.updateToExpanded() //get the cell and update it's constraints
    self.tableView.setNeedsDisplay()
    self.tableView.endUpdates()
}

而你使用

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
//... return required height
}

【讨论】:

  • tableView.setNeedsDisplay 没有帮助。动画工作后只有 tableView.reloadData()。
【解决方案3】:

我在使用 iOS 11 和自动调整大小/可调整大小的单元格时遇到了一些问题。我不知道 eactyl 对你来说有什么奇怪的行为,但在我的情况下,我确实将 tableView estimatedRowHeight 设置为大于我的最大单元格高度的值并且它解决了它。

【讨论】:

  • 我有不同的错误,我在问题评论中描述了它。但感谢您的意见。可能会帮助别人。
猜你喜欢
  • 1970-01-01
  • 2011-06-20
  • 2014-05-19
  • 2014-02-06
  • 1970-01-01
  • 2022-01-23
  • 1970-01-01
  • 1970-01-01
  • 2013-08-24
相关资源
最近更新 更多