Advertisement
Guest User

Swift-ViewController-MVP-sample

a guest
May 24th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 1.91 KB | None | 0 0
  1. //
  2. //  ViewController.swift
  3. //
  4.  
  5. import UIKit
  6.  
  7. protocol ListViewProtocol: class {
  8.     func refreshTableView()
  9. }
  10.  
  11. class ListViewController: UIViewController {
  12.    
  13.     // MARK: Properties
  14.    
  15.     @IBOutlet weak var tableView: UITableView!
  16.     var presenter: ListPresenterProtocol!
  17.    
  18.     // MARK: Lifecycle
  19.     override func viewDidLoad() {
  20.         super.viewDidLoad()
  21.        
  22.         tableView.delegate = self
  23.         tableView.dataSource = self
  24.        
  25.         presenter.didLoad()
  26.     }
  27.  
  28. }
  29.  
  30. extension ListViewController: ListViewProtocol {
  31.    
  32.     func refreshTableView() {
  33.         tableView.reloadData()
  34.     }
  35.    
  36. }
  37.  
  38. extension ListViewController: UITableViewDelegate {
  39.    
  40.     func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  41.         presenter.didPressed(at: indexPath.row)
  42.         tableView.deselectRow(at: indexPath, animated: true)
  43.     }
  44.    
  45. }
  46.  
  47. extension ListViewController: UITableViewDataSource {
  48.    
  49.     func numberOfSections(in tableView: UITableView) -> Int {
  50.         return 1
  51.     }
  52.    
  53.     func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  54.         return presenter.users.count
  55.     }
  56.    
  57.     func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  58.         let cell = tableView.dequeueReusableCell(withIdentifier: UserTableViewCell.cellIdentity,
  59.                                                  for: indexPath)
  60.        
  61.         let user = presenter.users[indexPath.row]
  62.         (cell as? UserTableViewCell)?.fillCell(user: user)
  63.        
  64.         return cell
  65.     }
  66.    
  67.     func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
  68.         let lastIndex = tableView.numberOfRows(inSection: 0) - 1
  69.         if indexPath.row == lastIndex {
  70.             presenter.didScrollToButtom()
  71.         }
  72.     }
  73.    
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement