2017-02-25 8 views
1

私は動的に詳細テキストラベルで設定した内容に基づいて行の高さを設定しようとしています、パートAに以下のコードを使用しては動的にコンテンツに基づいて、テーブルビューのセルの高さを調整 - iOSのスウィフトを

私は、以下のパートBに示すように、セルの詳細テキストラベルに数行のテキストを挿入しています。

私は他の同様の質問を見てきましたが、助けはありませんでした。

詳細テキストラベルの内容に基づいて行の高さを動的に調整する方法についてアドバイスをいただけますか。

パートA

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

はまた

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

パートB

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "daysIdentifier", for: indexPath) 

     cell.textLabel?.text = days[indexPath.row] 

     cell.detailTextLabel?.numberOfLines = 0 

     var joinedString   = self.availabilityTimeDict[dayName]?.joined(separator: " \n ") 
     cell.detailTextLabel?.text = joinedString 
     return cell 
    } 

enter image description here

+2

set label topとbottomは適切な制約を設定し、tableView.estimateRowHeight = 50も設定します。 –

答えて

8

使用カスタムセルとラベルを試してみました。 UILabelの制約を設定します。

tableView.estimatedRowHeight = 68.0 
tableView.rowHeight = UITableViewAutomaticDimension 

//委任&データソース

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

に(右上、左、下、)UILabelの セットラインは、のViewControllerのviewDidLoadメソッドで次のコードを追加します。

スウィフト4:

//デリゲート&データソース

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

所与の上部、下部、先頭とテーブルビュー

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

func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat 
{ 
return 100 
} 
0
  • まずセットアップカスタムセルの方法を以下tableview.Useのごlable内のコンテンツへの末尾とラベルを追加その行数をゼロに設定して、セルのコンテンツビューに下限、上端、下端の制約を与えます(高さを与えないでください)。また、サイズ検査でセルにカスタム高さを与え、次にviewDidLoadで行うだけです。

tableView.rowHeight = UITableViewAutomaticDimension tableView.estimatedRowHeight = 100.0

関連する問題