Answer the question
In order to leave comments, you need to log in
datasource not working?
Please tell me what could be the reason or where to dig.
The table is not updated for me, as if tableView.reloadData () does not work,
although the debugger seems to be normal and the movies array is rewritten to data from the Internet, but for some reason the dataSource is not called (What could be the reason, tell me plz
The architecture is something like this)
ViewModel -> Presenter -> ViewController
ViewModel -> DataSource
There is a ViewModel where I make a request to the server to get a list of movies
class ListMoviesViewModelTable: ListMoviesViewModelTableProtocol {
let network: Network = Network()
var movies: [Movie] = []
var numberPage: Int = 1
func fetchMovies(with page: Int, completion: @escaping () -> ()) {
network.numberPage = page
network.loadMovies { (result) in
switch result {
case .success(let result):
DispatchQueue.main.async {
self.movies.append(contentsOf: result.movies)
completion()
}
case .failure(let errorText):
print(errorText)
}
}
}
func numberOfCell() -> Int {
return movies.count
}
func cellForRowAt(index: Int) -> ListMoviesViewModelTableCellProtocol {
let movie = movies[index]
return ListMoviesViewModelTableCell(movie: movie)
}
func didSelectCell(at index: Int) -> DetailMovieViewModelProtocol {
let movie = movies[index]
return DetailMovieViewModel(movie: movie)
}
}
func fetchMovies() {
viewModelTable.fetchMovies(with: viewModelTable.numberPage) {
self.view.reloadData()
}
}
class ListMoviesViewController: UIViewController, ViewControllerProtocol {
@IBOutlet weak var tableView: UITableView!
var presenter: PresenterProtocol!
var dataSource: ListMoviesDataSource!
let configuration: ConfigurationProtocol = ListMoviesConfiguration()
override func viewDidLoad() {
super.viewDidLoad()
configuration.configurate(with: self)
delagating()
presenter.fetchMovies()
}
private func delagating() {
tableView.dataSource = dataSource
tableView.delegate = self
}
func reloadData() {
tableView.reloadData()
}
}
class ListMoviesDataSource: NSObject, UITableViewDataSource {
weak var view: ViewControllerProtocol!
var viewModelTable = ListMoviesViewModelTable()
var tableView: UITableView!
init(view: ViewControllerProtocol) {
super.init()
self.view = view
self.setTableView(from: view)
}
private func setTableView(from view: ViewControllerProtocol) {
tableView = view.getTableView()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return viewModelTable.numberOfCell()
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! ListMoviesTableViewCell
cell.viewModelCell = viewModelTable.cellForRowAt(index: indexPath.row)
cell.configure()
return cell
}
}
Answer the question
In order to leave comments, you need to log in
Good day. Of course, you will excuse me, but there is hardly a hero who can master these code pieces. I can only give a little advice, if your table or collection is not updated when you call reload, you need to look in the appropriate cell for item at methods.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question