我需要从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方法。我有两个屏幕。
我已经完成了列出数据。但点击我需要通过他传球。怎么做? 我无法理解它应该传递给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.
}
}