我試圖計算所有產品的產品價格總和。所以,我能夠獲得總數,但它正在產生輸出數組。但我只需要最後的總數。試圖計算Swift3 Tableview控制器中產品的價格
這是我的混帳回購協議:(github.com/sulkytejas/shopping-cart)
代碼:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cartCell", for: indexPath)
let order = orders?[indexPath.row]
let prices = [order?.product?.price]
print("in table view")
for price in prices {
total += Double(price!)
}
print (total)
cell.textLabel?.text = order?.product?.name
return cell
}
輸出:
in table view
1810636.0
in table view
1810676.0
in table view
1810766.0
in table view
1810806.0
in table view
1810821.0
in table view
1810911.0
in table view
1810951.0
您希望表中的所有訂單或每個訂單中的所有產品的總數是多少? (即一個總計,或每行一個)?我想你想要第一個,所以'cellForRowAt'不是做這個的好地方,因爲它將被調用用於表中的每一行,並且可能會不止一次地調用每一行。 – Paulw11
https:// github。com/sulkytejas/image-cart –