簡體   English   中英

滾動時如何更新UITableView數據

[英]how to update the UITableView data while scrolling

我已經在UITableView顯示了數據。該列表用於在購物車列表中添加購物車項目。

在此處輸入圖片說明

根據這張圖片可以看到以下細節:

  1. 價錢
  2. 產品名稱
  3. 步進加數量
  4. 總金額

因此,為此,我首先在UITableView添加了產品。 然后步進動作增加或減少數量,我根據給出了代碼。

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(carts: chartViewModel.datafordisplay(atindex: indexPath))

    cell.cartadd = { [weak self] in

        if let i = self?.tableView.indexPath(for: $0) {

            let productid = self?.chartViewModel.datafordisplay(atindex: indexPath)
            print(productid?.cartid)

            self?.chartViewModel.search(idsearch:productid?.cartid)
            print(self?.chartViewModel.searchindex(objectatindex: 0))
            cell.setcartData(cartsadd: (self?.chartViewModel.searchindex(objectatindex:0))!)
            print(cell.quantity)

            self?.chartViewModel.totalPriceInCart()
            let theIntegerValue1 :[Int] =  (self?.chartViewModel.totalvalue)!
            let result = "\(theIntegerValue1[0])"
            print(result)
            let theStringValue1 :String = String(describing: result)
            print(theStringValue1)

            self?.totalresult.text = theStringValue1
            print(i)

        }
    }

視圖模型:

class ChartViewModel: NSObject {


    var datasourceModel:ChartDataSourceModel

     var totalvalue:[Int] = []
    var insertedArray:CartModel? 

    var filteredListArray:Array<CartModel>? = []
    var productArray:CartModel?

     var finalListArray:Array<CartModel>? = []

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

    }
    func search(idsearch :String?) {

        filteredListArray = datasourceModel.dataListArray?.filter{($0.cartid?.range(of: idsearch!, options: .caseInsensitive) != nil)}
        print(filteredListArray)


    }


    func searchindex(objectatindex index: Int) ->  CartModel {

        return self.filteredListArray![index]

    }



    func datafordisplay(atindex indexPath: IndexPath) -> CartModel{

             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)
         print(datasourceModel.dataListArray)

    }

    func totalPriceInCart() {




        var totalPrice: Int = 0
        for product in datasourceModel.dataListArray! {

            totalPrice += product.cartsumint!

            print(totalPrice)


        }
        self.totalvalue = [totalPrice]

   }



    func insert(atIndex indexPath: IndexPath) {
        print(productArray)
        print(datasourceModel.dataListArray)
        datasourceModel.dataListArray!.insert(productArray!, at: indexPath.row)
        print(datasourceModel.dataListArray)
        self.datasourceModel.dataListArray = datasourceModel.dataListArray
        print(datasourceModel.dataListArray)
        self.finalListArray =   self.datasourceModel.dataListArray
    }

      func add()  {

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

            self.datasourceModel.dataListArray = datasourceModel.dataListArray

            print(insertedArray?.cartsum)

    }

 }

上面的代碼是添加數量。 並且此代碼在UITableView cellForRowAt委托中給出。 但是問題是:

-當我添加產品數量時,價格也會發生變化,並將顯示在UITableView 但是在滾動UITableView ,數據將像以前一樣顯示。

初始假設:-產品名稱:汽車,價格:300,數量:1

這將顯示在UITableView 當我添加數量:2,因此價格:600,這將顯示在UITableView 但是問題是在滾動UITableView它變成了價格:300,數量:1.那么如何更新UITableView 應該怎么做

UITableviewCell:-

 func setEventData(carts:QM_CartModel)
    {



        self.name.text = carts.cartname

        self.price.text = carts.carttotalprice


        self.itemQuantityStepper.value = 1

        setItemQuantity(quantity)



        print(self.price.text)


        let value = carts.cartsum



        let x: Int? = Int(value!)
        print(x)


        let add = x!
        print(add)


        let theIntegerValue1 :Int =  add
        let theStringValue1 :String = String(theIntegerValue1)
        // self.price.text = theStringValue1

        self.price.text = "QR \(theStringValue1)"
        print(self.price.text)


    }


    func setcartData(cartsadd:QM_CartModel)
    {

        let theIntegerValue :Int =  self.quantity
        let theStringValue :String = String(theIntegerValue)
        cartsadd.cartquantity  = theStringValue
        print(cartsadd.cartquantity)
        print(cartsadd.cartsum)

        let value = cartsadd.cartsum
        let qty = cartsadd.cartquantity

        let x: Int? = Int(value!)
        print(x)
        let y: Int? = Int(qty!)
        print(y)


        let add = x! * y!
        print(add)


        let theIntegerValue1 :Int =  add
        let theStringValue1 :String = String(theIntegerValue1)
       // self.price.text = theStringValue1


        self.price.text = "QR \(theStringValue1)"
        print(self.price.text)


        cartsadd.cartsumint = theIntegerValue1
      //  print(cartsadd.cartsum)

    }

您需要將數據(已更新)存儲在模型類中。因為再次滾動tableview dequeuereusablecell時,它將從模型中獲取數據,這就是您總是獲取舊數據的原因。

class TableViewCellData {

    var data: Any?
}

-

class TableViewCell: UITableViewCell {

    var cellData: TableViewCellData?
    {
        didSet {
            // change data in cell from TableViewCellData
        }
    }

    var cartadd: (() -> Void)?
}

-

class ViewController: UITableViewController {

    var dataSource: [TableViewCellData] = []

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "identifier", for: indexPath) as! TableViewCell
        cell.cellData = dataSource[indexPath.row]
        cell.cartadd = {
            cell.cellData?.data = "" // set data to TableViewCellData
        }
        return cell
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM