2016-10-23 20 views
1

複数のセクションを持ち、長いテキストのセクションがいくつかあるテーブルビューコントローラを使用しています。 titleForHeaderSectionを使用すると、テキストの長さがtableviewフレームよりも大きい場合、テキストは切り詰められます。私は次の行にテキストを表示したい。長いテキストのテーブルビューセクションヘッダーを複数の行に表示するにはどうすればよいですか?

答えて

2

titleForHeaderInSectionを使用する代わりに、viewForHeaderInSectionを試してみてください。このメソッドはビューを返すように求めますが、UILabelUIViewのサブクラスなので、ラベルを返すこともできます。

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 
    let label = UILabel() 
    label.backgroundColor = UIColor.black 
    label.textColor = UIColor.white 
    label.lineBreakMode = .byWordWrapping 
    label.numberOfLines = 0 
    label.text = "Here's to the crazy ones. The misfits. The rebels. The troublemakers. The round pegs in the square holes. The ones who see things differently. They're not fond of rules. And they have no respect for the status quo. You can quote them, disagree with them, glorify or vilify them. About the only thing you can't do is ignore them. Because they change things. They push the human race forward. And while some may see them as the crazy ones, we see genius. Because the people who are crazy enough to think they can change the world, are the ones who do." 
    return label 
} 

このアプローチを使用すると、テキストはテーブルビューの左右の端に移動します。ほとんどの場合、特にテーブルビューがデバイスの全幅を占める場合、これはあまり良くありません。

簡単な修正:UILabelのカスタムサブクラスを作成します(私はTableViewHeaderLabelと呼ぶ)。このサブクラスでは、ラベルにインセットを追加します。また、ViewControllerファイルをよりきれいにするために、単語の折り返し、背景色などをこのラベルに移動することもできます。

これはTableViewHeaderLabel.swiftは次のようになります。このようになり、それを使用して、その後

import UIKit 

class TableViewHeaderLabel: UILabel { 

    var topInset:  CGFloat = 0 
    var rightInset:  CGFloat = 12 
    var bottomInset: CGFloat = 0 
    var leftInset:  CGFloat = 12 

    override func drawText(in rect: CGRect) { 

     let insets: UIEdgeInsets = UIEdgeInsets(top: self.topInset, left: self.leftInset, bottom: self.bottomInset, right: self.rightInset) 
     self.setNeedsLayout() 

     self.textColor = UIColor.white 
     self.backgroundColor = UIColor.black 

     self.lineBreakMode = .byWordWrapping 
     self.numberOfLines = 0 

     return super.drawText(in: UIEdgeInsetsInsetRect(rect, insets)) 
    } 

} 

そして:ソリューション@Austinウッド

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 
    let label = TableViewHeaderLabel() 
    label.text = "Here's to the crazy ones. The misfits. The rebels. The troublemakers. The round pegs in the square holes. The ones who see things differently. They're not fond of rules. And they have no respect for the status quo. You can quote them, disagree with them, glorify or vilify them. About the only thing you can't do is ignore them. Because they change things. They push the human race forward. And while some may see them as the crazy ones, we see genius. Because the people who are crazy enough to think they can change the world, are the ones who do." 
    return label 
} 
+0

感謝を。最初の部分は私の問題を解決するために使った。しかし、私は2番目のソリューションを試していたが、下の2行は、ラベルが定義されていないと言ってエラーを投げています。viewForHeaderInSectionからラベル参照を取得していないと考えていました。 label.lineBreakMode = .byWordWrapping label.numberOfLines = 0 – TeKnofUNk

+0

申し訳ありません!これらの2つの行では、 "label"を "self"に変更してください。変更を反映するために私の回答を編集しました。 –

+0

"label"を "self"に変更すると、問題が修正されます。 – TeKnofUNk

1

viewForHeaderInSectionを使用すると、headerViewを作成できます。 UILabelをheaderviewにサブビューとして追加します。テキストを複数の行に表示するには、UILabelの複数行プロパティを使用します。また、headerViewの高さを設定します。

関連する問題