UITableView 显示循环的最后一个值

Posted

技术标签:

【中文标题】UITableView 显示循环的最后一个值【英文标题】:UITableView Showing last value of a loop 【发布时间】:2016-03-27 14:50:21 【问题描述】:

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

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

【问题讨论】:

只发布您遇到问题的代码。 我以前做过,没有人理解它,所以把完整的代码放上去有助于理解我的问题的全部意义。 我不明白你的问题,它对未来可能遇到同样问题的用户没有用处。你没有具体说明你遇到了什么问题。只应发布必要的代码以及您遇到的数学/编码问题。如果没有,希望其他人能理解。 ***.com/help/how-to-ask@yousefalenezi 我已经附上了 2 张带有代码的照片:@Pyro Table View with UITableView Table view with print() 【参考方案1】:

主要问题在于您的数据数组。

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

data = [interest]

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

你应该怎么做:

data.append(interest)

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

【讨论】:

【参考方案2】:

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 个值,因此它在每一行中显示相同的值

【讨论】:

我很高兴,这有帮助

以上是关于UITableView 显示循环的最后一个值的主要内容,如果未能解决你的问题,请参考以下文章

iOS:重新加载 UITableView 后滑动显示数据

从 XIB 加载的 UITableView Sectionheader 有时显示不正确

如何在 UITableView 中显示带有视频图像的视频文件?

将 UIButton 放入 UITableView

在后台删除的数据导致 UITableView 中的索引超出范围崩溃

在 uitableview 底部创建一个文本字段并发送按钮,例如 Instagram 评论部分