在自定义UITableViewCell中调整UITextView的大小

我有一个自定义的UITableViewCell ,我试图根据内容的大小调整其内部的UITextView 。 我在iOS7上,并使用Autolayout。

我已经尝试使用以下内容:

 [cell.question setScrollEnabled:YES]; [cell.question sizeToFit]; [cell.question setScrollEnabled:NO]; 

 - (CGRect)textViewHeightForAttributedText: (NSAttributedString*)text andWidth: (CGFloat)width { UITextView *calculationView = [[UITextView alloc] init]; [calculationView setAttributedText:text]; CGSize size = [calculationView sizeThatFits:CGSizeMake(width, FLT_MAX)]; CGRect finalFrame = CGRectMake(0, 0, width, size.height); return finalFrame; } 

来自不同的SOpost。 我可以调整框架。 但是问题是我无法看到变化。 从某种意义上说,当我login时,我可以看到变化。 但UITextView不会resize。 我无法find任何自动布局依赖关系。

当我禁用AutoLayout,它似乎工作。 我如何做到这一点,通过启用AutoLayout?

谢谢。

编辑

这是我的UITextView约束

截图

你必须做这个计算

 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath; 

方法,并相应地调整单元格的高度。

如果你懂了,没关系。 或者如果您需要代码示例,请再次询问。 我想你明白了!

更新

  - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { UITextView *tempTV = [[UITextView alloc] init]; [tempTV setText:@"your text"]; CGFloat width = self.tableView.frame.size.width - TEXT_ORIGIN_X - TEXT_END_X; CGSize size = [tempTV sizeThatFits:CGSizeMake(width, MAX_HEIGHT)]; return (TEXT_ORIGIN_Y + size.height + TEXT_BOTTOM_SPACE); } 

您可能会忘记实现TableView的delegete的heightForRowAtIndexPath方法;

 -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { CGFloat yourTextViewsHeight = ... calculate it here return yourTextViewsHeight; } 

我认为你可能对文本视图有太多限制。 我不能确定这一点,因为很难传达关于在IB中构build的约束的信息。

您的文本视图只需要两个约束,每个轴一个,以完全约束。 一个约束应该水平放置文本视图,另一个垂直放置。 自动布局系统将使用文本视图的内部内容大小自动生成文本视图的大小限制。

我认为现在有一些限制是阻止文本视图的大小调整。 发生这种情况是因为,默认情况下,您自己创build的约束是必需的(优先级为1000)。 另一方面,自动生成的大小限制具有较低的优先级,并且将被任何所需的冲突限制所推翻。

注意:仅仅因为文本视图只需要两个约束来完全约束并不意味着你不能有更多的约束。 包含4个标签,3个图像视图和1个文本视图的表格单元格是一个复杂的布局,因此您很可能会限制其他UI对象相对于文本视图而不是超级视图。

 I had the same issue but in a different situation. I have UItableview with two custom cells. First Custom cell - self expanding textview. (like email type message box) Second Custom Cell - Static image. Have a look at my code. You will get an insight to automatic resizing of cells. // ViewController.swift // ListWrap import UIKit class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate, UITextViewDelegate { @IBOutlet var listWrapTableView: UITableView! //CustomCells var CellIdentifier: String = "ListWrapTableViewCellID" var tapGesture: UITapGestureRecognizer! override func viewDidLoad() { super.viewDidLoad() } override func viewWillAppear(animated: Bool) { //Adding Tap Gesture To Table tapGesture = UITapGestureRecognizer(target: self, action: "tapRecognized:") self.listWrapTableView.addGestureRecognizer(tapGesture) tapGesture.cancelsTouchesInView = false tapGesture.enabled = true } func tapRecognized(recognizer: UITapGestureRecognizer){ self.listWrapTableView.endEditing(true) } func textViewDidBeginEditing(textView: UITextView) { if (CellIdentifier == "ListWrapTableViewCellID") { tapGesture.enabled = true } else { tapGesture.enabled = false } } // MARK: - Table view data source func numberOfSectionsInTableView(tableView: UITableView) -> Int { return 1 } func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { self.listWrapTableView.rowHeight = UITableViewAutomaticDimension return self.listWrapTableView.rowHeight } func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { return UITableViewAutomaticDimension } func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 2 } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { if (indexPath.row == 0) { let surveyCell = tableView.dequeueReusableCellWithIdentifier(CellIdentifier)! as! ListWrapTableViewCell return surveyCell } else if (indexPath.row == 1) { let reportsCell = tableView.dequeueReusableCellWithIdentifier("ListWrapSecondTableViewCellID")! as! ListWrapSecondTableViewCell return reportsCell } else { let cell:UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "") return cell } } func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { } } The first Custom cell: // ListWrapTableViewCell.swift // ListWrap import UIKit class ListWrapTableViewCell: UITableViewCell{ @IBOutlet var listWrapTextView: UITextView! // // override init?(style: UITableViewCellStyle, reuseIdentifier: String!) { // super.init(style: style, reuseIdentifier: reuseIdentifier) // } required init(coder aDecoder: NSCoder) { super.init(coder: aDecoder)! } /// Custom setter so we can initialise the height of the text view var textString: String { get { return listWrapTextView.text } set { listWrapTextView.text = newValue textViewDidChange(listWrapTextView) } } override func awakeFromNib() { super.awakeFromNib() listWrapTextView.scrollEnabled = false listWrapTextView.delegate = self } override func setSelected(selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated) if selected { listWrapTextView.becomeFirstResponder() } else { listWrapTextView.resignFirstResponder() } } } extension ListWrapTableViewCell: UITextViewDelegate { func textViewDidChange(textView: UITextView) { let size = textView.bounds.size let newSize = textView.sizeThatFits(CGSize(width: size.width, height: CGFloat.max)) // 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) if let thisIndexPath = tableView?.indexPathForCell(self) { tableView?.scrollToRowAtIndexPath(thisIndexPath, atScrollPosition: .Bottom, animated: false) } } } } extension UITableViewCell { /// Search up the view hierarchy of the table view cell to find the containing table view var tableView: UITableView? { get { var table: UIView? = superview while !(table is UITableView) && table != nil { table = table?.superview } return table as? UITableView } } } The second custom cell: // ListWrapSecondTableViewCell.swift // ListWrap import UIKit class ListWrapSecondTableViewCell: UITableViewCell { override func awakeFromNib() { super.awakeFromNib() } override func setSelected(selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated) } } attaching storyBoard for further reference. 

在这里输入图像说明

你的布局有点复杂,但是一切设置都正确无关。

你不必计算任何东西(通过使用sizeThatFits )。

所有你需要做的是禁用UITextView的滚动启用属性,然后在textViewDidChange调用tableView.beginUpdates()和tableView.endUpdates()。 这不会中断第一响应者,并顺利调整表视图的大小。

有关详细说明,请查看我写的post ,其中还包含一个正在运行的示例项目。

尝试这个

在你的自定义的UITableViewCell类中设置你的UITextViewsockets

 [yourTextView setAutoresizesSubviews:YES]; yourTextView.autoresizingMask = UIViewAutoresizingFlexibleWidth; 

希望这会为你工作