fork download
  1. import UIKit
  2.  
  3. import UIKit
  4.  
  5. class InnerTableCell: UITableViewCell {
  6.  
  7. override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
  8. super.init(style: style, reuseIdentifier: reuseIdentifier)
  9.  
  10. contentView.backgroundColor = .blue
  11. self.textLabel?.text = "hi(\(arc4random()%70)"
  12. }
  13.  
  14. required init?(coder aDecoder: NSCoder) {
  15. fatalError("init(coder:) has not been implemented")
  16. }
  17. }
  18.  
  19. //
  20. // MARK :- CELL
  21. //
  22. class CustomTableCell: UITableViewCell, UITableViewDelegate, UITableViewDataSource {
  23.  
  24. private let cellId = "cellId"
  25.  
  26. lazy var tableView: UITableView = {
  27.  
  28. let tv = UITableView(frame: .zero, style: .grouped)
  29. tv.translatesAutoresizingMaskIntoConstraints = false
  30. tv.backgroundColor = .green
  31. tv.delegate = self
  32. tv.dataSource = self
  33. tv.alwaysBounceVertical = false
  34. tv.isScrollEnabled = false
  35.  
  36. tv.separatorColor = .red;
  37. tv.separatorStyle = .none;
  38.  
  39. tv.register(InnerTableCell.self, forCellReuseIdentifier: self.cellId)
  40. return tv
  41. }()
  42.  
  43. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  44. return 10
  45. }
  46.  
  47. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  48. return 50
  49. }
  50.  
  51. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  52.  
  53. let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! InnerTableCell
  54. return cell
  55. }
  56.  
  57. func setupAutoLayout() {
  58.  
  59. tableView.leftAnchor.constraint(equalTo: contentView.leftAnchor).isActive = true
  60. tableView.rightAnchor.constraint(equalTo: contentView.rightAnchor).isActive = true
  61. tableView.topAnchor.constraint(equalTo: contentView.topAnchor).isActive = true
  62. tableView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor).isActive = true
  63. }
  64.  
  65. override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
  66. super.init(style: style, reuseIdentifier: reuseIdentifier)
  67.  
  68. contentView.backgroundColor = .white
  69. contentView.addSubview(tableView)
  70. setupAutoLayout()
  71. }
  72.  
  73. required init?(coder aDecoder: NSCoder) {
  74. fatalError("init(coder:) has not been implemented")
  75. }
  76. }
  77.  
  78.  
  79.  
  80. //
  81. // MARK :- ViewController
  82. //
  83. class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
  84.  
  85. private let cellId = "cellId"
  86.  
  87. lazy var tableView: UITableView = {
  88.  
  89. let tv = UITableView(frame: .zero, style: .grouped)
  90. tv.translatesAutoresizingMaskIntoConstraints = false
  91. tv.backgroundColor = .lightGray
  92. tv.delegate = self
  93. tv.dataSource = self
  94. tv.separatorStyle = .none
  95.  
  96.  
  97. tv.register(CustomTableCell.self, forCellReuseIdentifier: self.cellId)
  98. return tv
  99. }()
  100.  
  101.  
  102. //
  103. // MARK :- CELL
  104. //
  105. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  106.  
  107. return 2
  108. }
  109.  
  110. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  111. return 500
  112. // let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! CustomTableCell
  113. // return cell.tableView.contentSize.height
  114. }
  115.  
  116. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  117.  
  118. let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! CustomTableCell
  119. return cell
  120. }
  121.  
  122. override func viewDidLoad() {
  123. super.viewDidLoad()
  124.  
  125. title = "TableView Demo"
  126. view.backgroundColor = .white
  127. view.addSubview(tableView)
  128. setupAutoLayout()
  129. // UIScrollViewContentInsetAdjustmentBehavior
  130. tableView.contentInsetAdjustmentBehavior = .never
  131. automaticallyAdjustsScrollViewInsets = false
  132. }
  133.  
  134. func setupAutoLayout() {
  135.  
  136. tableView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
  137. tableView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
  138. tableView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
  139. tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
  140. }
  141. }
  142.  
  143.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.m:1:1: error: unknown type name ‘import’
 import UIKit
 ^~~~~~
prog.m:3:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘import’
 import UIKit
 ^~~~~~
prog.m:3:1: error: unknown type name ‘import’
prog.m:22:1: error: unknown type name ‘class’
 class CustomTableCell: UITableViewCell, UITableViewDelegate, UITableViewDataSource {
 ^~~~~
prog.m:22:22: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘:’ token
 class CustomTableCell: UITableViewCell, UITableViewDelegate, UITableViewDataSource {
                      ^
prog.m:83:1: error: unknown type name ‘class’
 class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
 ^~~~~
prog.m:83:21: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘:’ token
 class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
                     ^
stdout
Standard output is empty