UITableView显示循环的最后一个值

时间:2016-03-27 14:50:21

标签: ios arrays swift uitableview loops

我想在tableview中显示此循环的所有值。代码是计算贷款的摊销表。我尝试在数组中保存循环数据,但它总是给我最后的值。我真的陷入了困境。那我该怎么办呢?这是我的代码:

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()

2 个答案:

答案 0 :(得分:2)

主要问题在于您的数据阵列。

在您填充数据数组的循环中,在tableCalculation中,就是这样:

data = [interest]

这意味着,对于每次迭代,您数据数组设置为[interest],而不是新项目附加到数组。

你应该做什么:

data.append(interest)

请注意,您使用self.data2犯了同样的错误。但现在你知道如何解决这种错误了。

答案 1 :(得分:0)

cellForRowAtIndexPath方法中,您提供相同的数据进行打印,看起来像问题

我对所有标签都没有太多帮助,但您可以根据要求进行更改

对于lbl2和lbl4,你为所有行传递一个相同的浮点变量,这就是为什么它显示相同的值,如果你想为每一行显示不同的值,你应该将它存储在数组中cellForRowAtIndexPath就像你为lbl1做的那样

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

for lbl5你的代码单元代码应该是这样的

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

对于lbl 3&amp; lbl 5当我用静态值执行此代码以获得兴趣时,它只在数组中存储一个值

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

要存储您在数组中计算的每个值,您应该使用append

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

因为每个索引路径的数组中只有一个值,它根据你的模运算给出数组中的第0个值,所以它在每一行中显示相同的值

相关问题