单击mvvm中的删除按钮时如何删除单元格

时间:2018-05-02 05:05:39

标签: ios swift uitableview mvvm

我在UITableview中列出了数据。所以我得到了输出。输出是名称列表。

我已经在mvvm.Now我需要删除单元格,同时单击删除按钮,需要更新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)!
    }
}

我的DataSource模型类: -

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
    }

现在,点击删除按钮时如何执行删除方法。ModelViewviewController

中应添加所有代码

1 个答案:

答案 0 :(得分:0)

通常,在MVVM中,所有与UI相关的逻辑都应该在ViewModel中编写,View或ViewController只需要根据ViewModel显示UI,并将UI事件发送给ViewModel。当ViewModel发生更改时,它会发送事件或通知,因此View可以响应和更改。这叫做bind。同样,viewModel也应该绑定到Model。

在您的情况下,ViewModel应该有一个删除方法,就像这个

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == .delete {
        // pass delete cell event to viewModel
        chartViewModel.delete(atIndex: indexPath)
    }
}

所以视图可以将事件传递给它

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    NotificationCenter.default.addObserver(self, selector: #selector(viewModelDeleteRows(n:)), name: NSNotification.Name(rawValue: "DeleteAtRow"), object: chartViewModel)
}

@objc func viewModelDeleteRows(n: NSNotification) {
    if let i = n.userInfo?["indexPath"] as? IndexPath {
        // respond to viewModel changes and sync UI
        self.tableView.deleteRows(at: [i], with: .automatic)
    }
}

视图还需要绑定更改

var deleteCell : ((ChartCell) -> Void)?
@IBAction func clickDeleteButton(sender: UIButton) {
    deleteCell?(self)
}

此外,您可以将单元格删除事件传递给它的控制器,然后控制器可以传递给viewModel

cell.deleteCell = {[weak self] in
        if let i = self?.tableView.indexPath(for: $0) {
            self?.chartViewModel.delete(atIndex: i)
        }
}

并配置单元格

{{1}}