我在静态表观中有3个自定义单元格。所有细胞都有不同的高度。但是,我需要根据其内容大小来使NewsDetail TextView的高度动态。在TableView中,可以使标签动态很容易。但是它在静态表视图中的文本视图没有奏效。我搜索了,但找不到适当的解决方案,有人可以在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
}
}
首先,您需要确保为UITEXTVIEW禁用滚动。 commentsTextView.isScrollEnabled = false
然后,为UitextView设置了情节板中的自动层约束,但请确保不要为其高度设置一个。
您应该实现UITATION VIEWDATASOURCE方法heightForRowAt
和estimatedHeightForRowAt
或使用UITATION VIEWCONTROLLER对应属性。
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return 77
}
如果您的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)
}
}
您的文本视图不会使您的单元高度更大,因为文本视图将在其内容大小中增长。我的意思是它增加了他的滚动,而不是它的高度。因此,如果您的文本视图没有增加他的高度,那么您的动态单元格的高度也不会。
解决方案:使用具有行数= 0
您可以使用ptsmessagingcell
在您的资源中添加ptsmessagingcell.h和ptsmessagingcell.m文件。
#import "PTSMessagingCell.h"
在您的标题文件中。
然后实现 uitaiteViewDataSource 方法 height forrowarat 如下:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
{
let textSize = PTSMessagingCell.messageSize(newsDetail.text)
return textSize.height + 2*PTSMessagingCell.textMarginVertical() + 5.0
}