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

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

喜欢这个 : 在这里输入图像说明

这是我的表格视图代码:

 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 } 

在这里,我为tableview hight创build一个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 } } 

输出: