I am trying to implement UITableViewController using Model-View-Presenter. Since I am new to this concept, I am very confused by how to separate UITableView's data source protocols from UIViewController.

Many articles agree with the idea that View+Viewcontroller needs to be dumb and only consider about UIs and their layouts. On the other hand, Presenter needs to be a facilitator between view and model and also implement logic, but they stated to not include UIKit since it does not directly manipulate UIs. And here is where my confusion kicks in.

When we implement UITableView, we have to implement two protocols: UITableViewDelegate and UITableViewDataSource, especially the data source.

I assume delegate belongs to View portion and passes events to Presenter when receives user action.

However, to implement UITableViewDataSource, we must have some model that table view utilizes. That being said, I can only think of two ways to somehow fit into MVP.

  1. Pass Model value to UITableView from Presenter, and let it be read-only valuable in the view for the sake of data source (such as create a row for a specific index). But then I think it violates the MVP concept since the Model value is direct access by View.

  2. Let presenter be the data source. But since Presenter should not import UIKit, I think import UIKit and implement UITableViewDatasource also violates the MVP concept.

So long story short, I am confused where and how to appropriately implement UITableDatasource... If anyone knows this concept well, please share your knowledge. Thank you in advance!

0 Answers