2016-03-27 26 views
0

我想在tableview中顯示此循環的所有值。代碼是計算貸款攤銷表。我試着將循環的數據保存在數組中,但它總是給我最後的值。我真的被困在這。那麼請問我該怎麼做?這是我的代碼:UITableView顯示循環的最後一個值

import UIKit 

class tableViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { 

@IBOutlet var tableView: UITableView! 

var arr = [Int]() 
var cell:tableCell! 
var TPayment: float_t! // calls value of 59600 from main controller 
var years1: float_t! // number of months = 180 (15 years) 
var monthlyPayment: float_t! // 471 
var interest: float_t! // 5% 
var principil: float_t! //222 
var interestrate: float_t! // 249 
var initil: float_t! 
var data = Array<float_t>() 
var data2: NSMutableArray = [] 

override func viewDidLoad() { 
    super.viewDidLoad() 
    tableView.delegate = self 

    tableView.dataSource = self 

    let c = Int(years1) 
    arr += 0...c 

    tableCalculation() 

     // Register custom cell 
     let nib = UINib(nibName: "table", bundle: nil) 
     tableView.registerNib(nib, forCellReuseIdentifier: "cell") 
} 

func tableCalculation() { 
    let years = Int(years1) 
    initil = TPayment - 0 

    for i in 0..<years { 
      initil = initil - principil 
      interest = initil * interestrate     
      principil = monthlyPayment - interest     
      print("Month : \(monthlyPayment), principil: \(principil),interest: \(interest), initi: \(initil)") 
      data = [interest] 
      self.data2 = [initil] 
    }   
} 

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return arr.count 
} 

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell  { 

    cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! tableCell 
    cell.lbl1.text = "\(arr[indexPath.row])"   
    cell.lbl2.text = "\(monthlyPayment)" 
    cell.lbl3.text = "\(data[indexPath.row % data.count])" 
    cell.lbl4.text = "\(principal)" 
    cell.lbl5.text = "\(self.data2[indexPath.section])"   
    return cell   
}  

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    print("Row \(indexPath.row) selected") 
} 

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 
    return 40 
} 
} 

Table View with UITableView

Table view with print()

+1

僅發佈您遇到問題的代碼。 – lukaivicev

+0

我之前做過,沒有人知道它,所以放上完整的代碼將有助於理解我的問題的整個觀點。 –

+1

我不明白你的問題,對未來可能有同樣問題的用戶沒用。你沒有具體說明你有什麼問題。只有必要的代碼應該發佈以及你所遇到的數學/編碼問題。希望別人能理解,如果不是的話。 HTTP://計算器。COM /幫助/如何對問@yousefalenezi – lukaivicev

回答

2

的主要問題是與您的數據陣列。

在你的循環,你填入你的數據陣列,在tableCalculation,有這樣的:

data = [interest] 

這意味着對於每個迭代你設置數據陣列[interest]而不是附加新項目到陣列。

你應該做的,而不是:

data.append(interest) 

注意,你犯同樣的錯誤與self.data2。但是現在你知道如何解決這種錯誤。

+0

非常感謝你。它的工作現在 –

0

cellForRowAtIndexPath方法,則需要給予同樣的要打印的數據,看起來像問題

我沒有得到太多的所有標籤,但您可以根據需求

爲LBL2更改和lbl4,你爲所有行傳遞一個相同的浮點變量,這就是爲什麼它顯示相同的值,如果你想顯示每行的不同值,你應該把它存儲在數組中,並在cellForRowAtIndexPath得到它就像你正在做的lbl1

cell.lbl2.text = currencyFormatter(monthlyPayment) 
cell.lbl4.text = currencyFormatter(principil) 

爲lbl5您的代碼單元代碼應該是這樣的

cell.lbl5.text = "\(self.data2[indexPath.row])" 

對於LBL 3 & LBL 5,當我與靜態值執行此代碼數組中獲得利益,只存儲一個值

for i in 0..<years { 
    let interest = 5.0 * 4.0 
    data = [interest] 
} 

存儲您在陣列計算的每一個值,你應該使用append

data.append(interest) 
self.data2.append(initil) 

,因爲在只值1數組爲每個索引路徑,它根據您的模數運算給出數組中的第0個值,因此它在每一行中顯示相同的值

+0

非常感謝你。它的工作現在 –

+0

我很高興,這有助於 – HardikDG

相關問題