如何根据swift 3中静态表视图中的文本增加文本视图大小

Tam*_*nah 1 uitableview ios

我在静态 TableView 中有 3 个自定义单元格。所有单元格都有不同的高度。但是我需要根据其内容大小使 newsDetail TextView 的高度动态化。在 tableview 中为标签设置动态很容易。但是它不适用于静态 TableView 中的 TextView。我进行了搜索,但找不到合适的解决方案,请任何人都可以在 swift 3 中帮助我吗?我的代码:

import UIKit



class DetailTableView: UITableViewController {

@IBOutlet weak var myImageView: UIImageView!


@IBOutlet weak var newsHeadline: UILabel!


@IBOutlet weak var newsDetail: UITextView!

override func viewDidLoad() {
    super.viewDidLoad()

   newsDetail.text = "Barack Hussein Obama II (US: /b??r??k hu??se?n o??b??m?/ (About this sound listen) b?-RAHK hoo-SAYN oh-BAH-m?;[1][2] born August 4, 1961) is an American politician who served as the 44th President of the United States from 2009 to 2017. He is the first African American to have served as president. He previously served in the U.S. Senate representing Illinois from 2005 to 2008 and in the Illinois State Senate. Obama was born in 1961 in Honolulu, Hawaii, two years after the territory was admitted to the Union as the 50th state. Raised largely in Hawaii, Obama also spent one year of his childhood in Washington State and four years in Indonesia. After graduating from Columbia University in 1983, he worked as a community organizer in Chicago. In 1988 Obama enrolled in Harvard Law School, where he was the first black president of the Harvard Law Review. After graduation, he became a civil rights attorney and professor, and taught constitutional law at the University of Chicago Law School from 1992 to 2004. Obama represented the 13th District for three terms in the Illinois Senate from 1997 to 2004, when he ran for the U.S. Senate."


    tableView.estimatedRowHeight = 44
    tableView.rowHeight = UITableViewAutomaticDimension


}

override func numberOfSections(in tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return 3
}

public override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}
}
Run Code Online (Sandbox Code Playgroud)

小智 5

首先,您需要确保为 UITextView 禁用滚动。 commentsTextView.isScrollEnabled = false

然后,在故事板中为 UITextView 设置自动布局约束,但请确保不要为其高度设置一个。

您应该实现 UITableViewDataSource 方法heightForRowAt和/estimatedHeightForRowAt或使用 UITableViewController 相应的属性。

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

override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
    return 77
}
Run Code Online (Sandbox Code Playgroud)

如果您的 UITextView 是可编辑的,并且您希望它在您输入时动态调整大小,请将您的控制器textViewDidChange设为委托,并按如下方式实现:

extension ContactDetailsViewController: UITextViewDelegate {

// Resize textview depending on it's content
func textViewDidChange(_ textView: UITextView) {

    // Get textview content size
    let size = textView.bounds.size
    let newSize = textView.sizeThatFits(CGSize(width: size.width, height: .greatestFiniteMagnitude))

    // Resize the cell only when cell's size is changed
    if size.height != newSize.height {
        UIView.setAnimationsEnabled(false)
        tableView.beginUpdates()
        tableView.endUpdates()
        UIView.setAnimationsEnabled(true)

        let indexPath = IndexPath(row: 2, section: 0)
        tableView.scrollToRow(at: indexPath, at: .bottom, animated: false)
    }
}
Run Code Online (Sandbox Code Playgroud)