0

getting data from presenter in protocol not reflecting in tableview cell

class ViewController: UIViewController {
    
    @IBOutlet weak var tableView: UITableView!
    
    var presenter : Presenter?
    var products = [Products]()
    override func viewDidLoad() {
        super.viewDidLoad()
        
        presenter?.getDataFromInteractor()
        
    }
    
    func reloadTable(){
        DispatchQueue.main.async {
            self.tableView.reloadData()
        }
    }
}

extension ViewController:UITableViewDataSource,UITableViewDelegate,PresenterProtocol{
    
    func didFinishGettingDataFromPresenter(data: [Products]) {
        print(data)
        products = data
        reloadTable()
    }
    
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 100
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return products.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell",for: indexPath) as! TableViewCell
        cell.productName.text = products[indexPath.row].name
        cell.productProducer.text = products[indexPath.row].producer
        cell.productCost.text = "RS \(products[indexPath.row].cost)"
        return cell
    }
}

protocol PresenterProtocol {
    func didFinishGettingDataFromPresenter(data:[Products])
}

class Presenter:InteractorProtocol {
    var interactor : Interactor?
    var presenter : PresenterProtocol?
    // need data from interactor
    func getDataFromInteractor() {
        interactor?.fetch()
    }
    func didFinishGettingData(data: [Products]) {
        presenter?.didFinishGettingDataFromPresenter(data: data)
    }
}
DonMag
  • 69,424
  • 5
  • 50
  • 86
  • what is presenter ? full snippet of code ? – Zeeshan Ahmad II Jul 14 '22 at 07:32
  • protocol PresenterProtocol{ func didFinishGettingDataFromPresenter(data:[Products]) } class Presenter:InteractorProtocol{ var interactor : Interactor? var presenter : PresenterProtocol? // need data from interactor func getDataFromInteractor(){ interactor?.fetch() } func didFinishGettingData(data: [Products]) { presenter?.didFinishGettingDataFromPresenter(data: data) } } – smack bot Jul 14 '22 at 07:44
  • To start with... you declare an optional `var presenter : Presenter?` ... then in `viewDidLoad()` you try to ***use*** that optional with `presenter?.getDataFromInteractor()` -- but you never **instantiate** `presenter`. Looks like you're doing the same thing with `interactor`. I ***strongly suggest*** you start simpler and get to understand how to instantiate (create) objects and use them first. – DonMag Jul 14 '22 at 12:03
  • @DonMag Do you know Viper architecture? We have to assume that the presenter, interactor, and view controller were all instantiated and hooked up when the router / wireframe took us to this "module". – matt Jul 14 '22 at 12:21
  • 1
    @matt - whoops... missed the Viper tag :( – DonMag Jul 14 '22 at 12:27

0 Answers0