Cells are usually not displayed in MVVM APP (swift)

[ad_1]

I’m writing an software to show information from the server. Created a StartController class that solely shows knowledge from the ViewModel. There are not any warnings within the venture, however at startup, a clean display screen.

last class StartController: UIViewController {
    
    // MARK: - Non-public properties
    
    non-public var viewModel: ViewModel?
    non-public let identifier = "tapeCell"
    non-public let tableView = CustomTableView(body: .zero)
    
    // MARK: - Lifecycle
    
    override func viewDidLoad() {
        tremendous.viewDidLoad()
        setUpView()
        setUpConstraints()
        reloadViewModel()
    }
    
    // MARK: - Non-public capabilities
    
    non-public func reloadViewModel() {
        viewModel?.configData { [weak self] in
            DispatchQueue.primary.async {
                self?.tableView.reloadData()
            }
        }
    }
    
    non-public func setUpView() {
        view.backgroundColor = #colorLiteral(crimson: 1, inexperienced: 1, blue: 1, alpha: 1)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: identifier)
        view.addSubview(tableView)
    }
}

// MARK: - Delegate

extension StartController: UITableViewDelegate {
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 100
    }
}

// MARK: - DataSource

extension StartController: UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection part: Int) -> Int {
        viewModel?.numberOfRowsInSection() ?? 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: identifier, for: indexPath)
        let VM = self.viewModel?.articleAtIndex(indexPath.row)
        var content material = cell.defaultContentConfiguration()
        
        content material.textual content = VM?.title
        content material.secondaryText = VM?.writer
        cell.contentConfiguration = content material
        
        return cell
    }
}

All of the logic takes place contained in the ViewModel, downloading knowledge from the community and transferring it to the StartController. However apparently, no knowledge is being transmitted.

last class ViewModel {
    
    // MARK: - Non-public properties
    
    non-public let service = TapeService()
    non-public var knowledge: [Articles] = []
    
    // MARK: - Capabilities
    
    func configData(completion: @escaping() -> ()) {
        service.tapeAdd { [weak self] end in
            swap outcome {
            case .success(let articles):
                self?.knowledge = articles
            case .failure(let error):
                print(error.localizedDescription)
            }
        }
    }
    
    func numberOfRowsInSection() -> Int {
        return knowledge.depend
    }
    
    func articleAtIndex(_ index: Int) -> ArticleViewModel {
        let article = self.knowledge[index]
        return ArticleViewModel(article)
    }
}

// MARK: - ArticleViewModel

last class ArticleViewModel {
    non-public let article: Articles
    
    init(_ article: Articles) {
        self.article = article
    }
    
    var title: String {
        return self.article.title ?? " "
    }
    
    var writer: String {
        return self.article.writer ?? " "
    }
}

I’d be very grateful if you happen to might inform me what my mistake is and what must be carried out to show the info. Thanks!

[ad_2]

Leave a Reply