如何调整基于textview的表格单元格?

我有一个自定义的UITableViewCell UITextView 。 textview委托在tableviewcell自定义类中分配。 文本视图滚动被禁用。 文本加载到每个文本视图,是多行。 但是文本总是被剪切,因为单元格高度不会改变。

我有以下viewDidLoad的tableview控制器:

 tableView.estimatedRowHeight = 56.0 tableView.rowHeight = UITableViewAutomaticDimension 

任何想法,为什么这是行不通的?

尝试我的答案完美的工作。

 var record : NSArray = NSArray() var hight: CGFloat = 0.0 

把这段代码放在viewDidLoad()

 record = ["I have a UITextView in a custom UITableViewCell. The textview delegate is assigned in the tableviewcell custom class." ,"Textview scrolling is disabled. Text loads into each textview and is multiline. But the text is always clipped because the cell height doesn't change.","I have the following in viewDidLoad of the tableview controller:"," have a UITextView in a custom UITableViewCell. The textview delegate is assigned in the tableviewcell custom class.","Textview scrolling is disabled. Text loads into each textview and is multiline. But the text is always clipped because the cell height doesn't change.","I have the following in viewDidLoad of the tableview controller:","i just give you one link at put place i use label and you can now use your textview and give same constrain that i give in that link and try it so your problem will be solve","I have the following in viewDidLoad of the tableview controller:"]; func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return record.count } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("Textviewcell", forIndexPath: indexPath) let textview: UITextView = (cell.viewWithTag(5) as! UITextView) textview.text = record.objectAtIndex(indexPath.row) as? String return cell } func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { // 7.1> hight = self.findHeightForText(self.record.objectAtIndex(indexPath.row) as! String, havingWidth: self.view.frame.size.width - 10, andFont: UIFont.systemFontOfSize(14.0)).height return 44 + hight } func findHeightForText(text: String, havingWidth widthValue: CGFloat, andFont font: UIFont) -> CGSize { var size = CGSizeZero if text.isEmpty == false { let frame = text.boundingRectWithSize(CGSizeMake(widthValue, CGFloat.max), options: .UsesLineFragmentOrigin, attributes: [NSFontAttributeName: font], context: nil) size = CGSizeMake(frame.size.width, ceil(frame.size.height)) } return size } 

在Swift 3.0中

 func findHeightForText(text: String, havingWidth widthValue: CGFloat, andFont font: UIFont) -> CGSize { var size = CGSize.zero if text.isEmpty == false { let frame = text.boundingRect(with: CGSize(width: widthValue, height: CGFloat.greatestFiniteMagnitude), options: .usesLineFragmentOrigin, attributes: [NSFontAttributeName: font], context: nil) size = CGSize(width: frame.size.width, height: ceil(frame.size.height))//CGSizeMake(frame.size.width, ceil(frame.size.height)) } return size } 

这是一些屏幕截图。 故事板

与UITextView运行时tableview

你应该使用委托方法

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

设置行的高度。 然后,在该方法中,通过计算将占据空间的textview的高度来返回单元格的高度。 你应该使用这个function:

 -(CGFloat)heightForTextViewRectWithWidth:(CGFloat)width andText:(NSString *)text withBuffer:(float)buffer { UIFont * font = [UIFont fontWithName:@"YourFontName" size:15.0f]; // Replace with your font name and size NSDictionary *attributes = @{NSFontAttributeName: font}; // this returns us the size of the text for a rect but assumes 0, 0 origin, width is the width of that your text box will occupy. // Ex. If you text box has padding of 12 both trailing and leading to the cell, then width should be the cell's width minus 24. CGRect rect = [text boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading) attributes:attributes context:nil]; // return height of rect return rect.size.height; } 

这将确定您的文本视图将在单元格中占用的矩形。 返回的高度等于文本视图的高度,所以如果您希望单元格高于文本框,请添加该填充。

 -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { NSString * yourText = commentDict.text; // or however you are getting the text float widthPadding = 24.0f; float heightPadding = 16.0f; float height = [self heightForTextViewRectWithWidth: (tableView.frame.size.width - widthPadding) andText:yourText]; return height + heightPadding; } 

希望这有助于!