2016-10-26 3 views
0

私は製品名とその価格と数量の動的データを表示する1つのテーブルビューを持っています。私の必要なのは、私のテーブルビューに表示されたすべての製品の最後です。総製品は何でもよい。 10製品または2製品のように。これまでに何があったかもしれない。しかし、すべての製品のうち最後に3つの静的データを表示する必要があります。 SUBTOTAL, TAX, TOTALのように。そして、私は、すべての製品の製品数量x prodctコストを計算する必要があります。そして、私の小計、税金、合計金額を表示する必要があります。このよう動的テーブルビューのコンテンツに静的データを追加する方法。計算を実行する

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 
} 
+0

が難しいですか? 「numberOfRowsInSection」の意味で「静的」という意味の3行を静的に追加してください。「return self.Addtocartdata.count + 3」という3つの行が必要です。次に、最後の3行を調べて計算します。あなたが直面している問題は何ですか? – Janmenjaya

+0

私は静的データを追加する方法を知らない。計算を行う。これは、すべての製品の数量xコストを計算し、私の合計で表示する必要があります。 – mack

+0

@mackあなたが直面する問題は何ですか?この質問で –

答えて

1

私はテーブルビューの絶頂のために1 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 
     } 
    } 

出力:

関連する問題