Guest User

Untitled

a guest
Nov 17th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. struct TableRowDM {
  2. let title: String
  3. let subTitle: String
  4. let hidden: Bool
  5. }
  6.  
  7. let cellId: String = "CellView"
  8.  
  9. class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
  10.  
  11. @IBOutlet weak var tableView: UITableView!
  12.  
  13. let rowData: [TableRowDM] = [
  14. TableRowDM(title: "Row 1", subTitle: "Short title 1th row", hidden: false),
  15. TableRowDM(title: "Row 2", subTitle: "Short title 2th row", hidden: true),
  16. TableRowDM(title: "Row 3", subTitle: "Very long text in subtitle at 3th row to test text wrapping and growing subtitle height", hidden: false),
  17. TableRowDM(title: "Row 4", subTitle: "Long text in subtitle at 4th row", hidden: false),
  18. TableRowDM(title: "Row 5", subTitle: "Long text in subtitle at 5th row", hidden: true),
  19. TableRowDM(title: "Row 6", subTitle: "Long text in subtitle at 6th row", hidden: false),
  20. ]
  21.  
  22. override func viewDidLoad() {
  23. super.viewDidLoad()
  24. tableView.rowHeight = UITableViewAutomaticDimension
  25. tableView.estimatedRowHeight = 50
  26. tableView.tableFooterView = UIView(frame: CGRect.zero)
  27. tableView.register(UINib(nibName: cellId, bundle: nil), forCellReuseIdentifier: cellId)
  28. tableView.delegate = self
  29. tableView.dataSource = self
  30. }
  31.  
  32. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  33. let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! CellView
  34. let row: TableRowDM = self.rowData[indexPath.row]
  35.  
  36. cell.title.text = row.title
  37. cell.subtitle.text = row.subTitle
  38.  
  39. if row.hidden {
  40. // hide subtitle
  41. cell.subtitleHeight.isActive = true
  42. cell.titleToSubtitleGap.constant = 0
  43. } else {
  44. // show subtitle
  45. cell.subtitleHeight.isActive = false
  46. cell.titleToSubtitleGap.constant = 16
  47. }
  48. cell.setNeedsLayout()
  49. cell.layoutIfNeeded()
  50. return cell
  51. }
  52. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  53. return rowData.count
  54. }
  55. }
  56.  
  57. -----------------
  58. |
  59. - titleLabel ----
  60. |
  61. | <- titleToSubtitleGap
  62. |
  63. - subtitleLabel -
  64. |
  65. -----------------
Add Comment
Please, Sign In to add comment