2017-10-14 13 views
2

tableViewに配列をプログラムしました。セルがdetailTextLabelに複数のラインを使用する場合、ライン間のスペースは小さくなります。プログラム的にこの高さを上げる方法があるかどうかを知りたいですか?ここでは、私は配列のために使用しているサンプルコードです。detailTextLabelの高さをプログラム的に変更する方法

cell.textLabel?.text = self.filtered[indexPath.row].coptic 
cell.detailTextLabel?.text = self.filtered[indexPath.row].english 
cell.textLabel?.font = UIFont(name:"CS Avva Shenouda", size:30) 
cell.detailTextLabel?.font = UIFont(name: "Constantia", size:25) 
cell.textLabel?.numberOfLines = 0 
cell.detailTextLabel?.numberOfLines = 0 
cell.detailTextLabel?.textColor = UIColor.darkGray 

return cell 
+0

非常に良い提案は、カスタムテーブルビューのセルを使用し、あなたの要件に応じてそれを設計する – Krunal

+0

合意された、カスタムセルは、あなたが必要とするすべてのコントロールを提供します。 – Rob

答えて

2

私はロジック全体であり、コード全体ではありません。あなたは、テーブルビューを持っている必要がdetailTextLabel高

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat 
{ 
    return 50 // You should put your code or logic that dynamic height based on heigh of label. 
} 
2

に応じてセルの高さを管理することができ、コード

func height(withConstrainedWidth width: CGFloat, font: UIFont) -> CGFloat { 
     let constraintRect = CGSize(width: width, height: .greatestFiniteMagnitude) 
     let boundingBox = self.boundingRect(with: constraintRect, options: .usesLineFragmentOrigin, attributes: [NSFontAttributeName: font], context: nil) 

     return ceil(boundingBox.height) 
    } 

変更cellForRowAtIndexPath方法

cell.detailTextLabel?.numberOfLines = 0 
let height = height(withConstrainedWidth:200, font:YourFont) // change width and font as per your requirement 
cell.detailTextLabel?.frame = CGRect(x: cell.detailTextLabel?.frame.origin.x, y: cell.detailTextLabel?.frame.origin.y, width: cell.detailTextLabel?.frame.size.width, height: height) 

以下で、文字列の高さを得ることができます estimatedRowHeightおよびtableViewの高さをUITableViewAutomaticDimensionとして設定します。

関連する問題