【发布时间】:2018-05-02 05:05:38
【问题描述】:
我已经在UITableview 中列出了数据。所以我得到了输出。输出是名称列表。
我已经在 mvvm 中完成了。现在我需要在单击删除按钮时删除单元格并需要更新 tableview。
我的模型视图:-
class ViewModel: NSObject {
var datasourceModel:DataSourceModel
init(withdatasource newDatasourceModel: DataSourceModel) {
datasourceModel = newDatasourceModel
}
func datafordisplay(atindex indexPath: IndexPath) -> Model{
return datasourceModel.dataListArray![indexPath.row]
}
func numberOfRowsInSection(section:Int) -> Int {
return (datasourceModel.dataListArray?.count)!
}
}
我的数据源模型类:-
class DataSourceModel: NSObject {
var dataListArray:Array<Model>? = []
init(array :Array<[String:Any]>?) {
super.init()
var newArray:Array<[String:Any]> = []
if array == nil {
newArray = self.getJsonDataStored22()
}
else {
newArray = array!
}
var datalist:Array<Model> = []
for dict in newArray {
let model = Model(dictionary: dict)
datalist.append(model!)
}
self.dataListArray = datalist
}
}
typealias dummyDataSource22 = DataSourceModel
extension dummyDataSource22 {
func getJsonDataStored22() ->Array<Dictionary<String,String>>{
let jsonArray = [["name":"Dosa Fest"],["name":"Organic Vegan Fest"],["name":"Food Of Life Time"],["name":"Tea Time"],["name":"Dosa Fest"],["name":"Organic Vegan Fest"],["name":"Food Of Life Time"],["name":"Tea Time"]] as Array<Dictionary<String,String>>
return jsonArray
}
}
还有我的 UIViewcontroller:-
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return chartViewModel.numberOfRowsInSection(section: section)
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let identifier = "cell"
var cell:ChartCell! = tableView.dequeueReusableCell(withIdentifier: identifier) as? ChartCell
if cell == nil {
tableView.register(UINib(nibName: "ChartCell", bundle: nil), forCellReuseIdentifier: identifier)
cell = tableView.dequeueReusableCell(withIdentifier: identifier) as? ChartCell
}
cell.setEventData(charts: chartViewModel.datafordisplay(atindex: indexPath))
return cell
}
我的 UITableviewcell:-
@IBOutlet weak var name: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
func setEventData(charts:Model) {
self.name.text = charts.restaurantname
}
那么现在如何在单击删除按钮时执行删除方法。ModelView 和 viewController 中应添加的所有代码
【问题讨论】:
-
如何在 mvvm 方法中删除单元格
-
如果你使用 mvvm,你应该向你的 viewModel 发送像删除单元格这样的 ui 事件。 viewModel 改变自己并发送通知。然后 view(viewController) 响应它并更改 UI。
-
@SolaWing 怎么办
-
@SolaWing 应该在视图模型和视图控制器中编写什么函数
-
任何人都可以解决问题
标签: ios swift uitableview mvvm