单击mvvm中的单元格时传递数据

时间:2018-04-07 06:20:03

标签: ios mvvm

我需要从tableview和我的代码中选择单元格如下:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
    let vc = OfferDetailViewController(nibName: "OfferDetailViewController", bundle: nil)
    let indexPath = (self.tableView.indexPathForSelectedRow)!
    let offerViewModel1 = self.offerViewModel.sourceAt(index: indexPath.row)
    vc.offerViewModel2 = offerViewModel1
    navigationController?.pushViewController(vc, animated: true)
}

我正在使用mvvm方法。我有两个屏幕。

  1. 在tableview中列出数据
  2. 从tableview中选择单元格时,必须转到其他屏幕。它包含详细信息。
  3. 我已经完成了列出数据。但点击我需要通过他传球。怎么做? 我无法理解它应该传递给viewmodel或viewcontroller。

    foodviewmodel: -

    class foodViewModel: NSObject {
    
        var datasourceModel:foodDataSource
    
        init(withdatasource  offerDatasourceModel: foodDataSource) {
            datasourceModel = offerDatasourceModel
        }
    
    
        func datafordisplay(atindex indexPath: IndexPath) -> foodModel{
            return  datasourceModel.dataListArray![indexPath.row]
    
        }
    
    
       func numberOfRowsInSection(section:Int) -> Int {
    
          return (datasourceModel.dataListArray?.count)!
      }
    
    
        //indexpath.....
       // func sourceAt(atindex indexPath: IndexPath) -> foodModel {
          //  return self.datasourceModel.dataListArray![indexPath.row]
       // }
    
        func sourceAt(index :Int) -> foodModel  {
            return self.datasourceModel.dataListArray![index]
        }
    }
    

    foodDataSource .swift: -

      var dataListArray:Array<foodModel>? = []
    
    
        init(array :Array<[String:Any]>?) {
            super.init()
            var newArray:Array<[String:Any]> = []
            if array == nil{
    
                newArray = self.getJsonDataStored4()
            }
            else{
                newArray = array!
    
            }
    
            var datalist:Array<foodModel> = []
            for dict in newArray{
    
                let model = foodModel(dictionary: dict)
    
                datalist.append(model!)
            }
            self.dataListArray = datalist
        }
    
    
    
    }
    
    typealias dummyDataSource4 = foodDataSource
    extension dummyDataSource4{
    
        func getJsonDataStored4() ->Array<Dictionary<String,String>>{
    
            let jsonArray = [["id":"1","name":"Lunch Buffet","price":"Q28","location":"Doha, Qatar","imageurl":"","offertype":"Today's Offer","restaurtantname":"MOMS KITCHEN"],["id":"2","name":"Economy Meal","price":"Q65","location":"Doha, Qatar","imageurl":"","offertype":"Tomorrow's Offer","restaurtantname":"Mr.Shawarma"],["id":"3","name":"Sit-Down Buffet","price":"Q65","location":"Doha, Qatar","imageurl":"","offertype":"Weekend's Offer","restaurtantname":"Maharaja Darbar"],["id":"4","name":"Lunch Buffet","price":"Q28","location":"Doha, Qatar","imageurl":"","offertype":"Today's Offer","restaurtantname":"Shircz Garden"]] as Array<Dictionary<String,String>>
    
            return jsonArray
        }
    
    
        Then i created fooddetailmodel,fooddetaildatasource,fooddetailviewmodel classes.
    

    但我不知道该传递什么

    我已经给出了代码: -

    var offerViewModel2 :QM_OfferModel!
    

    // @IBOutlet private weak var tableView:UITableView!

     override func viewDidLoad() {
            super.viewDidLoad()
    
            // Do any additional setup after loading the view.
        }
    
     }
    

0 个答案:

没有答案