【问题标题】:How to add static data in dynamic table view content. And perform calculation如何在动态表格视图内容中添加静态数据。并进行计算
【发布时间】:2016-10-26 06:06:36
【问题描述】:

我有一个表格视图,它将显示产品名称及其价格和数量的动态数据。我需要的是,最后在我的表格视图中显示的所有产品。总产品可以是任何东西。喜欢 10 个产品或 2 个产品。它可能是什么。但最后我必须展示 3 个静态数据。喜欢SUBTOTAL, TAX, TOTAL。我需要计算所有产品的产品数量 X 产品成本。在我的小计,税,​​totla 中,我需要显示总金额。

像这样:

这是我的表格视图代码:

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

let cell = tableView.dequeueReusableCell(withIdentifier: "cartcel", for: indexPath) as! cartTableViewCe
cell.productName.text = Addtocartdata[indexPath.row].cartproName
cell.productQty.text = Addtocartdata[indexPath.row].cartproPrice
cell.productAmount.text = Addtocartdata[indexPath.row].cartproPrice
return cell;
} 
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {       
 return self.Addtocartdata.count
}

【问题讨论】:

  • 那里有什么困难?只需静态添加 3 行,这意味着在“numberOfRowsInSection”“return self.Addtocartdata.count + 3”中,您总是知道您需要这 3 行。然后通过检查最后 3 行来进行计算。提及您具体面临的问题是什么?
  • 我不知道如何添加静态数据。并做计算。这必须计算所有产品的数量 x 成本,并且必须显示在我的总数中。
  • @mack 你面临什么问题?在这个问题中
  • 实际看,我想添加 3 个静态名称,如小计、税收、总计。它应该看起来像单表视图。而且我不知道如何为此执行计算。就像我需要计算所有产品的数量 x 成本,我需要在表格视图的总单元格中显示
  • @mack 你有多少数量和税价的价值吗?小计是多少?

标签: ios json swift


【解决方案1】:

在这里,我为 tableview 高度创建了一个 NSlayoutConstrain。

self.tableviewhightconstrain.constant = self.tableView.contentSize.height // give value according to item


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if self.Addtocartdata.count == 0 {
            return 0
        }
        return self.Addtocartdata.count + 1
    }



 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.row < self.Addtocartdata.count {
                let cell = tableView.dequeueReusableCell(withIdentifier: "cartcel", for: indexPath) as! cartTableViewCell
                cell.productName.text = Addtocartdata[indexPath.row].cartproName
                cell.productQty.text = Addtocartdata[indexPath.row].cartproPrice
                cell.productAmount.text = Addtocartdata[indexPath.row].cartproPrice
                return cell;
            }
            else{ // here is second cell that i create ok.
                 var total11 : Double = 0.0
                let totalitem : Int = self.Addtocartdata.count as Int
                for item in 0...totalitem - 1 {
                    let subtotal = 0.0

                   total11 = subtotal +  Double(self.Addtocartdata[item].cartproPrice!)!

                }
                print(total11)
                let totalcell = tableView.dequeueReusableCell(withIdentifier: "totalcell", for: indexPath)
                let subtotal : UILabel = totalcell.viewWithTag(5) as! UILabel

                subtotal.text = "$ \(total11)"
                let tax : UILabel = totalcell.viewWithTag(6) as! UILabel
                tax.text = "$ 125" // here give your tax 
                let finaltotal : Double = total11 + 125 // also add that text value here

                let total : UILabel = totalcell.viewWithTag(7) as! UILabel
                 total.text = "$ \(finaltotal)"

                return totalcell

            }
  }



 func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath.row < Addtocartdata.count {
            return 70
        }else{
            return 134
        }
    }

输出:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-23
    相关资源
    最近更新 更多