如何在动态表视图内容中添加静态数据。并执行计算 [英] How to add static data in dynamic table view content. And perform calculation
问题描述
我有一个表视图,它将显示产品名称及其价格和数量的动态数据。我需要的是,最后在我的表视图中显示的所有产品。总产品可以是任何东西。喜欢10个产品或2个产品。它可能是什么。但最后所有产品我必须显示3个静态数据。喜欢 SUBTOTAL,TAX,TOTAL
。我需要计算所有产品的产品数量X产品成本。在我的子总额中,tax,totla我需要显示总金额。
I have one table view which will display the dynamic data of product name and its price and qty. What i need is , at last of all product showned in my table view. The total product can be any thing. Like 10 products or 2 product. What ever it may be. But at last of all product i have to show the 3 static data. Like SUBTOTAL, TAX, TOTAL
. And i need to calculate the product qty X prodct cost of all product. And in my sub total, tax, totla i need to display the total amount.
像这样:
Like this :
这是我的表格视图代码:
And this is my table view code :
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创建了一个NSlayoutConstrain。
Here i create one NSlayoutConstrain for tableview hight .
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
}
}
输出:
这篇关于如何在动态表视图内容中添加静态数据。并执行计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!