UITableView中显示循环的最后一个值 [英] UITableView Showing last value of a loop

查看:114
本文介绍了UITableView中显示循环的最后一个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示回路中的tableview的所有值。在code是来计算表摊销贷款。我试图保存在数组中循环的数据,但它总是给我的最后一个值。我真的卡住了这一点。那么,如何能做到这一点吗?这是我的code:

I would like to show all the values of this loop in the tableview. The code is to calculate an Amortization Table for loans. I tried saving the data of the loop in the array, but it always gives me the last values. I really got stuck on this. So how can I do that, please? This is my code:

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

表视图与UITableView的

表视图()

推荐答案

的主要问题是与您的数据阵列。

The main issue is with your data array.

在你的循环,你填充您的数据阵列,在 tableCalculation ,有这样的:

In your loop where you populate your data array, in tableCalculation, there's this:

data = [interest]

这意味着对于每个迭代您的设置的数据阵列 [利益] 代替的追加的新项到阵列

It means that for each iteration you set the data array to [interest] instead of appending the new item to the array.

你应该做的,而不是:

data.append(interest)

需要注意的是你与 self.data2 同样的错误。但现在你知道如何解决这样那样的错误。

Note that you make the same mistake with self.data2. But now you know how to fix this kind of error.

这篇关于UITableView中显示循环的最后一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆