uikitviper-architecture

ViewController see count of object in Presenter, but don't display them


I'm refactoring my test project from SwifUI to UIKit and at the beginning a was though that I just need to keep my Viper modules as they are and change view, but I have no idea why my TableViewController see (tested with print(), every time view appears it shows that data were loaded) count of objects in Presenter, but don't display them. I also tried to check does my cell and tableview at all works and with custom data it works fine.

class BeersListTableViewController: UITableViewController,BeersListViewProtocol {
    var presenter: BeersListPresenterProtocol!
    var currentPage: Int = 1
    
    override func viewDidLoad() {
        super.viewDidLoad()
        navigationController?.navigationBar.prefersLargeTitles = true
        title = "All beers"
        let nib = UINib(nibName: "BeerCell", bundle: nil)
        self.tableView.register(nib, forCellReuseIdentifier: "BeerCell")
    }
    override func viewWillAppear(_ animated: Bool) {
        presenter.loadList(at: currentPage)
        print(presenter.beers.count)
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return presenter.beers.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "BeerCell", for: indexPath) as! BeerCell
        
        let beer = presenter.beers[indexPath.row]
        cell.beerName.text = beer.name
        cell.beerABV.text = presenter.formattedABV(beer.abv)
        cell.addToFavoritesButton.setImage(UIImage(systemName: presenter.favoritesButton(beer)), for: .normal)
        return cell
    }




class BeersListPresenter: BeersListPresenterProtocol{
    
    weak var view: BeersListViewProtocol!
    var interactor : BeersListInteractorProtocol!
    var router:  BeersListRouterProtocol!
    
    
    var beers = [Beer]()
    
    //MARK: - Load list of beers
    func loadList(at page: Int){
        interactor.loadList(at: page) { beers in
            DispatchQueue.main.async {
                self.beers.append(contentsOf: beers)
            }
        }
    }

Solution

  • Solved. May be somebody will help my solution, I created method in my view controller that call tableview.reloadData() every time presenter property is changed

    UPD: In view controller added a function:

    func reload(){
    tableView.reloadData()
    }
    

    In presenter changed:

    var beers = [Beer]{
    didSet{
       view.reload()
      }
    }