2017-05-26 5 views
1

Ich habe Textansichten in einer statischen Tabelle. Ich möchte, dass sie die Größe ändern, wenn ein Zeilenumbruch erforderlich ist. Wie mache ich das? Das ist bisher mein Code.iOS statische Tabelle automatische Größenanpassung basierend auf TextView

override func viewDidLoad() { 
    super.viewDidLoad() 

    table.estimatedRowHeight = 40.0 // Replace with your actual estimation 
    table.rowHeight = UITableViewAutomaticDimension 


    // Tap to dismiss keyboard 
    let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(EditInfoViewController.dismissKeyboard)) 
    view.addGestureRecognizer(tap) 


} 


func dismissKeyboard() { 
    view.endEditing(true) 
    // Save data 
} 



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

override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat { 
    return UITableViewAutomaticDimension 
} 

enter image description here

Antwort

3

Swift 3 & Xcode 8.3.2

Verwenden UILabel anstelle von UITextView und setzen numberOfLine = 0 ist, so wird es automatisch Resize entsprechend seinem Inhalt

oder

wenn Sie UITextView statt UILabe behalten möchten l, hier ist der Code

class YourClass: UITableViewController, UITextViewDelegate { 

    var yourCustomCell: UITableViewCell = UITableViewCell() 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     table.estimatedRowHeight = 40.0 // Replace with your actual estimation 
     table.rowHeight = UITableViewAutomaticDimension 


     // Tap to dismiss keyboard 
     let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(EditInfoViewController.dismissKeyboard)) 
     view.addGestureRecognizer(tap) 

     // Add tableView delegate 
     tableView.dataSource = self 
     tableView.delegate = self 

     // Add textView delegate 
     yourTextView.delegate = self 


    } 
    // Text view delegate, dont forget to add yourTextView.delegate = self in viewDidLoad 
    func textViewDidChange(_ textView: UITextView) { 
     if textView == yourTextView { 
      let newHeight = yourCustomCell.frame.size.height + textView.contentSize.height 
      yourCustomCell.frame.size.height = newHeight 
      updateTableViewContentOffsetForTextView() 
     } 
    } 
    // Animate cell, the cell frame will follow textView content 
    func updateTableViewContentOffsetForTextView() { 
     let currentOffset = tableView.contentOffset 
     UIView.setAnimationsEnabled(false) 
     tableView.beginUpdates() 
     tableView.endUpdates() 
     UIView.setAnimationsEnabled(true) 
     tableView.setContentOffset(currentOffset, animated: false) 
    } 


    // UITableViewDelegate, UITableViewDataSource 
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 
     return UITableViewAutomaticDimension 
    } 



    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { 
     return UITableViewAutomaticDimension 
    } 

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return 1 
    } 

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = yourCustomCell 
     cell.selectionStyle = .none 
     return cell 
    } 
} 

Das Ergebnis ist hier: Before the code

Ergebnis nach textViewDelegate verwenden und individuelle Größenanpassung Funktion After the code

+0

Ich weiß nicht, wie ein Etikett machen edditable so Ich habe versucht, die textView-Größe zu ändern. Ich bin mir nicht sicher, was ich falsch mache. So setze ich den delegierten TextView: titleTextView.delegate = self as? UITextViewDelegate – alvarlagerlof

+0

@Ideaman in Ihrer AnsichtDidLoad deklarieren Sie diese "titleTextView.delege = self", und in Ihrer Klasse fügen Sie diese Klasse myViewController: UIViewController, UITextViewDelegate {} –

+0

Es möchte ich es in UITextViewDelegate – alvarlagerlof

Verwandte Themen