如何在动态表视图内容中添加静态数据。并执行计算

时间:2016-10-26 06:06:37

标签: ios json swift

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

像这样: enter image description here

这是我的表格视图代码:

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
}

1 个答案:

答案 0 :(得分:1)

这里我为tableview hight创建了一个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
        }
    }

输出: