2017-10-14 157 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

我只是把我的邏輯,而不是整個代碼。 您可以通過下面的代碼

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) 

您可以根據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

表視圖需要有一個管理單元高度得到串的高度estimatedRowHeight和tableView高度爲UITableViewAutomaticDimension。

相關問題