-1

I'm trying to call viewModel.userDidSelectItem(identifier: "DetailMainViewController") function when the user clicks on a cell but I'm not able, first print(indexPath.row) is nil and second I don't know if its correct the call.

import Foundation import UIKit class MainViewController: UIViewController, UITableViewDataSource { @IBOutlet var tableview:UITableView! var viewModel: MainViewModel = MainViewModel() override func viewDidLoad() { super.viewDidLoad() viewModel.loadUsers { self.tableview?.reloadData() } } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return viewModel.fakeUsers?.count ?? 0 } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) ->UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "fakeUserObjectCell") as! MainTableViewCell cell.fakeUserObject = viewModel.fakeUsers?[(indexPath as NSIndexPath).row] return cell } func tableView(_ tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { print(indexPath.row) viewModel.userDidSelectItem(identifier: "DetailMainViewController") } } 
0

1 Answer 1

0

when you are using the tableview you need to set the UITableViewDataSource and UITableViewDelegate to the MainViewController. And override the below function

func numberOfSections(in tableView: UITableView) -> Int { return 1 } 
Sign up to request clarification or add additional context in comments.

2 Comments

You do not need to override numberOfSections(:)
If this was related to the problem then there would have been no cells to try to select.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.