mercredi 29 juillet 2015

MVVM Best practice in Swift

I have serious doubts on how in deep we have to use view model.

Let me explain with an example, simple view controller with a table view inside and a related view model.

class FooViewController: UIViewController, UITableViewDelegate, UISearchBarDelegate {

     let viewModel = FooViewModel()

     @IBOutlet var tableView: UITableView!

     override func viewDidLoad() {
        super.viewDidLoad() 
        tableView.delegate = self
        tableView.dataSource = self
     }

    // MARK: - Table view delegates -

    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return viewModel.heightForRow()
    }    
}

class FooViewModel {

    // MARK: - Table view management -

    func heightForRow() -> CGFloat {
        return 60
    }

}

Is a correct practice to delegate to the view model, for example, the height of each cell?
Generally, is ok delegate to view model UI "decision" on its aspect?

Aucun commentaire:

Enregistrer un commentaire