【问题标题】:how to update the UITableView data while scrolling如何在滚动时更新 UITableView 数据
【发布时间】:2018-05-25 05:31:33
【问题描述】:

我已在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)

    }

【问题讨论】:

    标签: ios swift uitableview


    【解决方案1】:

    您需要将数据(更新的)存储在模型类中。因为在滚动 tableview 时再次调用 dequeuereusablecell 它将从模型中获取数据,这就是您总是获取旧数据的原因。

    【讨论】:

    • 请检查,我已经更新了一些我已经完成的代码。那么根据那个如何更新?
    • @brucecraigb 正如我之前阅读您的问题时,您为控制器分配了新值..您需要做的是您必须访问您的特定数组行(您必须将 currentIndexpath 发送到您的每个 IBAction控制器)并在添加新值特定数组后,您只需要重新加载 UITableView
    【解决方案2】:
    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
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-10-14
      • 1970-01-01
      • 1970-01-01
      • 2013-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多