2016-08-04 20 views
4

動的セル高さを設定する方法を示すいくつかのチュートリアルを見てきましたが、適切な制約を設定して動的セルを使用している場合はすべてUITableViewAutomaticDimension 。しかし、私は静的な細胞のためにこれをしたいと思います。Swiftを使って動的セル高さで静的セルを作成する方法

タイトルがあり、説明テキストの内容とサイズが異なる開示インジケータが表示されているカテゴリを表示するセルがいくつか含まれています。したがって、私は、セルの高さを説明テキストのサイズごとに動的にしたいと思います。

私がスタティックセルを使用しているのは、ダイナミックセルを使用していない理由は、カテゴリセルの上にスタティックセルとストーリーボードを使用することによってしか得られないデザインのセルがいくつかあるからです。

私はiOS9Xcode 7.3Swift 2.2

UPDATE

表ビューコントローラコード

import UIKit 

class CategoriesTableViewController: UITableViewController { 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     tableView.rowHeight = UITableViewAutomaticDimension 
     tableView.estimatedRowHeight = 140 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    // MARK: - Table view data source 

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
     // #warning Incomplete implementation, return the number of sections 
     return 1 
    } 

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     // #warning Incomplete implementation, return the number of rows 
     return 2 
    } 

} 

UPDATE 2

01を使用しています

以下の2つの方法を私のテーブルビューコントローラに追加し、viewDidLoad()の2行を削除しました。これは私のためにやった!各セルのすべてのラベルに、上端、下端、先頭と末尾の制約が使用されていることを確認する必要がありました。また、各ラベルの行数が0

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

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

答えて

7

UILabelnumberOfLinesを設定する必要があります。さて、あなたのテキストビューを選択し、下の画像のようにスクロール可能なチェックを解除しましょう。

enter image description here

はあなたのビューコントローラに以下のコードを追加します。

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

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

私はテキストビューを使用していません。私はテキストのために通常のラベルを使用しています。 – Jace

+1

しかし、2つの方法を追加すると問題が解決しました。ラベルテキストのサイズに応じてセルのサイズが変更されるようになりました。ありがとう! – Jace

+0

オハイオ州。ウェルカムフレンド:)コンパイラを幸せにしてください! –

1

に設定する必要がありますあなたがしなければならないcell内部AutoLayout適切な設定なので、説明のテキストの長さに基づいて高さが増加しています。 は、その後、あなたのviewDidLoad

tableview.rowHeight = UITableViewAutomaticDimension 
tableview.estimatedRowHeight = 44 

で次のように設定する必要がありますのでご注意ください:あなたは、各セルのテキストビューを使用している0

3

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

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

@Forgeは、あなたのトピックに
スウィフト4変異体をしてください追加スウィフト3の場合:

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

override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat { 
    return UITableViewAutomaticDimension 
} 
関連する問題