如何根据 UITextView 子视图的大小调整 UITableViewCell 的大小?
How do I resize a UITableViewCell according to the size of a UITextView subView?
目前我可以根据其中的文本量成功调整 UITextView 的大小。
我的问题是这个 UITextView 在 UITableViewCell 中。我试图做的是使用调整后的 UITextView 的高度通过访问它的框架并设置它的高度来调整单元格的大小。
这是我的代码:
//dynamically resize textview and cell based on content
self.aboutMeTextView.text = profile["aboutMe"] as String
self.aboutMeTextView.sizeToFit()
self.aboutMeTextView.layoutIfNeeded()
var frame = self.aboutMeTextView.frame as CGRect
frame.size.height = self.aboutMeTextView.contentSize.height
self.aboutMeTextView.frame = frame
var cellFrame = self.aboutMeCell.frame as CGRect
cellFrame.size.height = self.aboutMeTextView.contentSize.height * 2
self.aboutMeCell.frame = cellFrame
只是不能正常工作。 textView 调整大小但单元格没有正确调整大小而且我的 scrollView 甚至不会向下滚动到足以让我看到整个调整大小的 textview。我猜如果能成功设置cells的高度,scrollView的高度会自动调整。
我看过类似的问题,但它们对我没有帮助。
非常感谢您的帮助。
感谢您的宝贵时间
您可以使用 UITableView
方法 heightForRowAtIndexPath
它returns 一行的高度。所以你可以使用当前单元格的标签并将单元格的高度设置为标签的高度:
override func tableView(tableView: UITableView!, heightForRowAtIndexPath indexPath: NSIndexPath!) -> CGFloat {
var cellID = "Cell"
var cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier(cellID) as UITableViewCell
var yourLabelHeight = cell.yourLabel.size.height
return yourLabelHeight
}
您可以像这样调用 UITableView.beginUpdates() 和 UITableView.endUpdates():
extension TableViewCell: UITextViewDelegate {
var delegate: TableViewController!
var row: Int!
func textViewDidChange(_ textView: UITextView) {
delegate.tableView.beginUpdates()
delegate.tableView.endUpdates()
}
}
class TableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
tableView.rowHeight = 44
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 44
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell
cell.delegate = self
cell.row = indexPath.row
return cell
}
}
确保在 table 视图单元格中为文本视图设置了约束。
目前我可以根据其中的文本量成功调整 UITextView 的大小。
我的问题是这个 UITextView 在 UITableViewCell 中。我试图做的是使用调整后的 UITextView 的高度通过访问它的框架并设置它的高度来调整单元格的大小。
这是我的代码:
//dynamically resize textview and cell based on content
self.aboutMeTextView.text = profile["aboutMe"] as String
self.aboutMeTextView.sizeToFit()
self.aboutMeTextView.layoutIfNeeded()
var frame = self.aboutMeTextView.frame as CGRect
frame.size.height = self.aboutMeTextView.contentSize.height
self.aboutMeTextView.frame = frame
var cellFrame = self.aboutMeCell.frame as CGRect
cellFrame.size.height = self.aboutMeTextView.contentSize.height * 2
self.aboutMeCell.frame = cellFrame
只是不能正常工作。 textView 调整大小但单元格没有正确调整大小而且我的 scrollView 甚至不会向下滚动到足以让我看到整个调整大小的 textview。我猜如果能成功设置cells的高度,scrollView的高度会自动调整。
我看过类似的问题,但它们对我没有帮助。
非常感谢您的帮助。
感谢您的宝贵时间
您可以使用 UITableView
方法 heightForRowAtIndexPath
它returns 一行的高度。所以你可以使用当前单元格的标签并将单元格的高度设置为标签的高度:
override func tableView(tableView: UITableView!, heightForRowAtIndexPath indexPath: NSIndexPath!) -> CGFloat {
var cellID = "Cell"
var cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier(cellID) as UITableViewCell
var yourLabelHeight = cell.yourLabel.size.height
return yourLabelHeight
}
您可以像这样调用 UITableView.beginUpdates() 和 UITableView.endUpdates():
extension TableViewCell: UITextViewDelegate {
var delegate: TableViewController!
var row: Int!
func textViewDidChange(_ textView: UITextView) {
delegate.tableView.beginUpdates()
delegate.tableView.endUpdates()
}
}
class TableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
tableView.rowHeight = 44
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 44
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell
cell.delegate = self
cell.row = indexPath.row
return cell
}
}
确保在 table 视图单元格中为文本视图设置了约束。