Advertisement
Don_Mag

Untitled

May 3rd, 2021
1,360
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 2.65 KB | None | 0 0
  1. class TransactionCell: UITableViewCell {
  2. }
  3.  
  4. class TransactionsHeaderView: UITableViewHeaderFooterView {
  5.     static let reuseId = "CustomHeader"
  6. }
  7.  
  8. class TransactionsViewController: UIViewController {
  9.  
  10.     let cellId = "cellID"
  11.     let numSections: Int = 4
  12.    
  13.     lazy var tableView = UITableView()
  14.    
  15.     override func viewDidLoad() {
  16.         super.viewDidLoad()
  17.        
  18.         commonInit()
  19.     }
  20.    
  21.     private func commonInit() {
  22.        
  23.         //view.backgroundColor = .clear
  24.        
  25.         tableView.backgroundColor = .systemBackground
  26.         tableView.rowHeight = 74
  27.         tableView.sectionHeaderHeight = 50
  28.        
  29.         tableView.register(TransactionCell.self, forCellReuseIdentifier: cellId)
  30.         tableView.register(TransactionsHeaderView.self, forHeaderFooterViewReuseIdentifier: TransactionsHeaderView.reuseId)
  31.        
  32.         tableView.delegate = self
  33.         tableView.dataSource = self
  34.        
  35.         tableView.contentInsetAdjustmentBehavior = .never
  36.         tableView.separatorInset = UIEdgeInsets(top: 0, left: 32, bottom: 0, right: 16)
  37.         tableView.tableFooterView = UIView()
  38.        
  39.         view.addSubview(tableView)
  40.        
  41.         tableView.translatesAutoresizingMaskIntoConstraints = false
  42.        
  43.         let g = view.safeAreaLayoutGuide
  44.        
  45.         NSLayoutConstraint.activate([
  46.            
  47.             tableView.topAnchor.constraint(equalTo: g.topAnchor, constant: 20.0),
  48.             tableView.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 20.0),
  49.             tableView.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: -20.0),
  50.             tableView.bottomAnchor.constraint(equalTo: g.bottomAnchor, constant: -20.0),
  51.            
  52.         ])
  53.  
  54.     }
  55.    
  56. }
  57.  
  58. extension TransactionsViewController: UITableViewDelegate, UITableViewDataSource, UIScrollViewDelegate {
  59.    
  60.     func numberOfSections(in tableView: UITableView) -> Int {
  61.         return numSections // self.sections.count
  62.     }
  63.    
  64.     func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  65.        
  66.         return 4 //sections[section].transactions.count
  67.     }
  68.    
  69.     func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  70.        
  71.         guard let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as? TransactionCell  else {
  72.             fatalError("The dequeued cell is not an instance of HomeTableViewCell.")
  73.         }
  74.         return cell
  75.     }
  76.    
  77.     func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
  78.         print(#function, section)
  79.        
  80.         let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: TransactionsHeaderView.reuseId) as! TransactionsHeaderView
  81.         header.contentView.backgroundColor = .orange
  82.         return header
  83.     }
  84.    
  85.     func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  86.         print(#function, section)
  87.         return 50
  88.     }
  89.    
  90. }
  91.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement