Come ridimensionare la cella della tabella basata su textview?

Ho un UITextView in un UITableViewCell personalizzato. Il delegato textview è assegnato nella class personalizzata tableviewcell. Lo scorrimento di Textview è disabilitato. Il testo viene caricato in ogni vista testo ed è multilinea. Ma il testo è sempre troncato perché l’altezza della cella non cambia.

Ho il seguente in viewDidLoad del controller tableview:

 tableView.estimatedRowHeight = 56.0 tableView.rowHeight = UITableViewAutomaticDimension 

Qualche idea sul perché questo non funziona?

Prova perfettamente la mia risposta.

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

Metti questo codice in 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 } 

In 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 } 

Ecco alcune schermate. storyboard

Vista tabella di runtime con UITextView

È necessario utilizzare il metodo delegato

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

per impostare l’altezza della riga. Quindi, all’interno di quel metodo, restituisci l’altezza della cella calcolando l’altezza della textview che occuperà lo spazio. Dovresti usare la funzione:

 -(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; } 

Questo determinerà il rect che la tua vista testuale occuperà nella cella. L’altezza che viene restituita è uguale all’altezza della visualizzazione del testo, quindi se vuoi che la cella sia più alta della casella di testo, aggiungi quella spaziatura.

 -(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; } 

Spero che questo ha aiutato!