2016-04-17 5 views
0

Im сталкивается с проблемой при настройке UITextview в UITableViewCell. Поскольку функция heightforrowatindexpath работает до cellforrowatindexpath, поэтому я не могу рассчитать размер содержимого UITextview.Высота UITextview в UITableview swift

Im вычисляет размер просто textview.contentSize.height в cellforrowatindexpath функция.

Im основном хотят UITextview contentHeight вписывается в UITableViewCell поэтому нет прокрутки не видна для каждого UITextView и он покажет полное содержание за счет увеличения высоты UITableViewCell.

Я также UILabel чуть выше UITextView в UITableViewCell, и я попытался установить противопоказания для обоих, но не смог достичь своей цели.

Как я могу вычислить размер UITableViewCell динамически в следующих функциях:

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 

} 

PS: Можно ли использовать увеличивайте UITableViewCell высоты немного в зависимости от высоты устройства, используя автоматическую раскладку или какой-то технику еще ,

ответ

3

Вы можете просто передать свой TextView с содержанием, чтобы вычислить высоту TextView

func calculateHeight(textView:UITextView, data:String) -> CGRect { 

    var newFrame:CGRect! 

    // I was using the HTML content here. If your content is not HTML you can just pass the stringValue to your textView 
    do { 
     let attr = try NSAttributedString(data: html.dataUsingEncoding(NSUTF16StringEncoding)!, options: [NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType], documentAttributes: nil) 
     textView.attributedText = attr 

    }catch { 
     //Handle Exceptions 
    } 

    let fixedWidth = textView.frame.size.width 
    textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.max)) 
    let newSize = textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.max)) 
    newFrame = textView.frame 
    newFrame.size = CGSize(width: max(newSize.width, fixedWidth), height: newSize.height) 
    print("height \(newFrame.height)") 
    return newFrame 
} 

Вы можете вызвать этот метод в heightForRowAtIndex так, что в ответ вы можете получить новый обновленный кадр, из которого вы можете получить новую обновленную высоту.

Убедитесь, что держать scrollEnabled в false из TextView вашей клетки в cellForRowAtIndexPath

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

     //Cell making here...... 

     cell.yourTextView.scrollEnabled = false 
     return cell 
    } 
} 
1

1) В heightForRowAtIndexPath дают размер ячейки следующие действия:

вернуться UITableViewAutomaticDimension

2) ... и добавьте следующее:

переопределить func tableView (tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { возвращение UITableViewAutomaticDimension }

Я надеюсь, что это помогает ...