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

아주 멋진 제안, 사용자 정의 tableview 셀을 사용하여 요구 사항에 따라 디자인하십시오. – 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) 
    } 

변경 당신은 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

에 따라 셀 높이를 관리 할 수 ​​있습니다

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) 

cellForRowAtIndexPath 방법으로 문자열의 높이를 얻을 수 있습니다 estimatedRowHeight 및 tableView 높이를 UITableViewAutomaticDimension으로 설정합니다.