如何在添加对象后更新tableview

时间:2018-05-16 08:19:37

标签: ios

如何在添加对象后更新tableview。

我在mvvm中做,

所以

在cartviewmodel中的

: -

var datasourceModel: DataSourceModel


var insertedArray: Model? 

var finalArray: Array<Model>? = []


func add()  {

            datasourceModel.dataListArray?.append(insertedArray!)
            print(datasourceModel.dataListArray)

            self.datasourceModel.dataListArray = datasourceModel.dataListArray

    }

在tableview单元格中,我使用了一个按钮进行添加。

所以在viewcontroller中我已经对添加按钮进行了操作 因此它将添加并显示另一个viewcontroller。

我的hotelviewcontroller为: -

添加按钮操作的代码......

cell.cartaddCell = {[weak self] in
             if let i = self?.tableView.indexPath(for: $0) {


            let cartDataSource:DataSourceModel = DataSourceModel(array: nil)
            let cartViewModel:ViewModel = ViewModel(withdatasource: cartDataSource)


            let cartmodel:Model = Model(withoffermodel:self!.offerViewModel.datafordisplay(atindex: indexPath))


            cartViewModel.insertedArray = cartmodel
            print(cartViewModel.insertedArray)
            cartViewModel.add()


            let cartViewController:ViewController  = ViewController(nibName: "ViewController", bundle: nil, withViewModel: cartViewModel)


            self?.navigationController?.pushViewController(cartViewController, animated: true)
            // self?.present(cartViewController, animated: true, completion: nil)
            // print(cartViewModel.insertedArray )

            print(cartmodel.offerprice)
            print(cartmodel.offerdetailAddName)
            print(cartmodel)
            print(i)
           // self?.chartViewModel.delete(atIndex: i)
       }
    }

现在在我的cartviewmodel中: -

class ViewModel: NSObject {


    var datasourceModel:DataSourceModel


    var insertedArray:Model? 

    var finalArray:Array<Model>? = []


    init(withdatasource  newDatasourceModel: DataSourceModel) {
        datasourceModel = newDatasourceModel
        print(datasourceModel.dataListArray)

    }


    func datafordisplay(atindex indexPath: IndexPath) -> Model{
        return  datasourceModel.dataListArray![indexPath.row]

    }

    func numberOfRowsInSection(section:Int) -> Int {

        return datasourceModel.dataListArray!.count
    }

    func delete(atIndex indexPath: IndexPath) {

        datasourceModel.dataListArray!.remove(at: indexPath.row)

    }

    func add()  {

            datasourceModel.dataListArray?.append(insertedArray!)
            print(datasourceModel.dataListArray)

      self.datasourceModel.dataListArray = datasourceModel.dataListArray


    }




    func insert(){
      add()
    datasourceModel.dataListArray!.insert(insertedArray?.offerdetailAddName, at: indexPath.row)

    }
}

我的cartviewcontroller: -

class ViewController: UIViewController ,UITableViewDataSource,UITabBarDelegate{

    @IBOutlet private weak var tableView: UITableView!
    @IBOutlet weak var orderbutton: UIButton!

    @IBOutlet weak var displayresult: UIView!



    private var chartViewModel :ViewModel!

    init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?, withViewModel viewModel:ViewModel) {

        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)

        chartViewModel = viewModel
    }



    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }


    override func viewDidLoad() {
        super.viewDidLoad()

        self.tableView.backgroundColor = UIColor(red: 0.93, green: 0.86, blue: 1, alpha:1.0)


        tableView.dataSource = self

        displayresult.isHidden = false



        self.tableView .reloadData()
        self.tableView.tableFooterView = displayresult


    }




    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))




     print(chartViewModel.insertedArray)


               return cell
    }





   func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {


        if editingStyle == .delete {

            chartViewModel.delete(atIndex: indexPath)
            tableView.deleteRows(at: [indexPath], with: .automatic)
            self.tableView.reloadData()
        }

    }


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


}

实际上我有2个屏幕。 1.hotelscreen 2.cartscreen

因此,在点击hoteltableviewcell时,有一个按钮可以添加购物车。

因此,添加酒店模型时会在购物车屏幕中添加。

因为我已经获得了价值。

但我的问题是: -

当我点击购物车屏幕时,不会显示添加的对象。

那么如何显示更新的数据。

1 个答案:

答案 0 :(得分:0)

似乎所有你需要的是tableView.reloadData()