2016-10-13 87 views
0

我試圖計算所有產品的產品價格總和。所以,我能夠獲得總數,但它正在產生輸出數組。但我只需要最後的總數。試圖計算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 
+2

您希望表中的所有訂單或每個訂單中的所有產品的總數是多少? (即一個總計,或每行一個)?我想你想要第一個,所以'cellForRowAt'不是做這個的好地方,因爲它將被調用用於表中的每一行,並且可能會不止一次地調用每一行。 – Paulw11

+0

https:// github。com/sulkytejas/image-cart –

回答

0

您需要更改orders.swift文件中的readOrdersFromArchive函數。如果您沒有訂單記錄,則此函數始終返回零值。這就是爲什麼您不能在addToCartPressed函數的orders數組中添加任何順序的原因。

class func readOrdersFromArchive() -> [Order]? 
{ 
    return NSKeyedUnarchiver.unarchiveObject(withFile: archiveFilePath()) as? [Order] ?? [Order]() 
} 

在添加下面的代碼後,我可以看到總值。

var total = 0.0 


override func viewWillAppear(_ animated: Bool) { 
    super.viewWillAppear(animated) 

    orders = Orders.readOrdersFromArchive() 

    if let orders = orders { 

     for order in orders { 

      total += (order.product?.price!)! 

      } 
     } 

     print(total) 

} 

我剛把for循環從cellForRowAtIndexPath移到了viewWillAppear。它不是一個合適的地方來計算總值,因爲每次tableview配置它的單元格時都會調用它。

+0

我使用seque來傳遞數據。我已經在購物車視圖控制器類中聲明瞭總數,並試圖在cellview函數中添加tableview。 –

+0

加法確實有效,但它產生了多個輸出。我只想要最終的輸出。我嘗試從View didl加載加載輸出,但它不顯示在控制檯中。它給出了空白輸出 –

+0

cartVC是否有順序數組作爲局部變量?您可以在ProductVC中選擇產品後打印出傳遞的對象值嗎?如何將代碼放在viewWillAppear函數中? – woogii

1
let prices = [order?.product?.price] 

這是怎麼回事在這?這看起來像一個訂單隻有一個產品?並且您將一個產品的價格包裝在數組中,從而產生一個單一價格的數組?

我不知道你的界面在這裏什麼,但似乎你想要的東西更像:

let prices = order.products.map() { $0.price } 

這將讓所有的產品陣列,然後讓每個價格的新數組產品。

+0

我目前有4種產品,每種產品都有一個名爲product.price的對象屬性。 –

+0

我目前有4種產品,每種產品都有一個名爲product.price的對象屬性。所以在ProductViewController中有一個addToCart按鈕。當你按下該按鈕時,產品對象被髮送到cartViewController。我在哪裏添加所有的產品價格以提供總價。 –

0

你的輸出數組都來自你的代碼正在運行您在您的表視圖,從您的輸出似乎是7

如果您只想輸出最終總那麼你就需要有每個單元調用

print (total)

tableView()功能。一個理想的地方將在viewDidLoad()函數中。

+0

我試圖加載從viewDidLoad中和viewWillApprear,但它不工作 –

+0

在任何你已經創建了'orders'對象,你應該把你的for-in循環和'打印()'聲明 – Danoram

+0

難道是... \t \t 這是我的git repo:[link(github.com/sulkytejas/shopping-cart)] ...如果你可以看看 –

0

總計應該在tableview委託之外進行計算,因爲如@ Paulw11所述,每次滾動或查看tableview時都會調用每行。最好的選擇是創建一個方法來計算模型本身的總數,或者是從你填充表格的位置向模型的擴展。

這裏是orders的數據類型是什麼?

+0

訂單的數據類型是陣列 –

+0

什麼是當你嘗試打印總內部viewWillAppear –

+0

是的,所以我在CartViewController中填充表,但添加到在ProductVC –

0
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]  
    let localTotal:Double = 0.0  

    print("in table view") 
    for price in prices { 
    localTotal += Double(price!) 
    }  
    print (localTotal) 
total = localTotal 
    print (total) 
    cell.textLabel?.text = order?.product?.name 
    return cell 
}