无法使用ViewModel加载数据

时间:2018-05-28 21:55:57

标签: swift mvvm tableview viewmodel

  • tableView dataSource在IB
  • 中正确设置
  • 在IB
  • 中也正确设置了viewController标识

这是我的 viewModel

class StatusCodeViewModel {

    let apiClient = APIClient.shared
    var statusCodes: [StatusCode] = []
    let identifier = "statusCodeCell"

    init() {}


    func loadStatusCodes() {
        apiClient.execute(service: .statusCode) { statusCodes in
            self.statusCodes = statusCodes
        }
    }

}

和我要加载数据的 viewController

class ViewController: UIViewController {

    @IBOutlet weak var tableView: UITableView!
    var viewModel: StatusCodeViewModel? {
        didSet {
            if viewModel!.statusCodes.count > 0 {
                self.tableView.reloadData()
            }
        }
    }


    override func viewDidLoad() {
        super.viewDidLoad()
        viewModel = StatusCodeViewModel()
        viewModel!.loadStatusCodes()
    }
}

extension ViewController : UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if let statusCodes = viewModel!.statusCodes as? [StatusCode] {
            return statusCodes.count
        }
        return 0
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: viewModel!.identifier)
        cell?.textLabel!.text = viewModel!.statusCodes[indexPath.row].title
        return cell!
    }


}

数据计数为0且tableView

中未显示任何数据

2 个答案:

答案 0 :(得分:0)

您已经在初始化时设置了视图模型。

当api返回调用时,你必须实现某种回调 - 最简单的方法是协议。

protocol StatusCodeViewModelDelegate {
    func callFinished()
}

class StatusCodeViewModel {

    let apiClient = APIClient.shared
    var statusCodes: [StatusCode] = []
    let identifier = "statusCodeCell"
    var delegate : StatusCodeViewModelDelegate?

    init() {}


    func loadStatusCodes() {
        apiClient.execute(service: .statusCode) { statusCodes in
            self.statusCodes = statusCodes
            delegate?.callFinished()
        }
    }

}

然后在你的viewController中:

override func viewDidLoad() {
    super.viewDidLoad()
    viewModel = StatusCodeViewModel()
    viewModel.delegate = self
    viewModel!.loadStatusCodes()
}

func callFinished() {
    self.tableView.reloadData()
}

不要忘记为你刚才做的代表延长:

class ViewController: UIViewController, StatusCodeViewModelDelegate {

或者,正如@rmaddy建议的那样,在View模型中将loadStatusCodes更改为:

func loadStatusCodes(completion: @escaping () -> Void) {
    apiClient.execute(service: .statusCode) { statusCodes in
        self.statusCodes = statusCodes
    }
}

然后,在viewDidLoad中:

override func viewDidLoad() {
    super.viewDidLoad()
    viewModel = StatusCodeViewModel()
    viewModel!.loadStatusCodes {
        self.tableView.reloadData()
    }
}

答案 1 :(得分:0)

//这可以!

     func loadStatusCodes(completion: @escaping () -> Void) {
               apiClient.execute(service: .statusCode) { statusCodes in
                   self.statusCodes = statusCodes
                   completion()
               }
           }
           

//在ViewController中:

               override func viewDidLoad() {
                       super.viewDidLoad()
                       viewModel = StatusCodeViewModel()
                       viewModel?.loadStatusCodes() {
                           self.tableView.reloadData()
                       }
                   }