2017-05-26 21 views
1

静的テーブル内にテキストビューがあります。改行が必要なときにサイズを変更したい。私はこれをどのようにして行うのですか?これはこれまでの私のコードです。iOS静的テーブルテキストベースの自動サイズ変更

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

答えて

3

スウィフト3 &のXcode 8.3.2

使用代わりUITextViewのUILabel、及び設定numberOfLine = 0、自動サイズ変更の内容に応じますので

または

代わりにUITextViewを使用したい場合は、UILabe私はラベルを作るedditableようにする方法がわからない Before the code

結果機能 After the code

+0

をtextViewDelegateを使用して、カスタムリサイズ後:lは、ここにコード

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

結果はこちらです私はtextViewのサイズを変更するために行った。私は何が間違っているのか分かりません。これは、私がtextViewのデリゲートを設定する方法です: titleTextView.delegate = self as? UITextViewDelegate – alvarlagerlof

+0

あなたのviewDidLoadの@Ideamanは、この "titleTextView.deleget = self"を宣言し、yourClassにこのクラスを追加しますmyViewController:UIViewController、UITextViewDelegate {} –

+0

それをUITextViewDelegateにキャストします。 – alvarlagerlof

関連する問題