smack bot
smack bot

Reputation: 1

Getting Data from Presenter No reflecting in View Controller Tableview Cell

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)
    }
}

Upvotes: 0

Views: 146

Answers (0)

Related Questions