UITableView Showing last value of a loop

后端 未结 2 476
有刺的猬
有刺的猬 2021-01-26 06:09

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, bu

相关标签:
2条回答
  • 2021-01-26 06:39

    In the cellForRowAtIndexPath method you are giving the same data to print that looks like the issue

    I don't get much for all the labels but you can change it based on the requirement

    for lbl2 and lbl4 you are passing a same single float variable for all the rows that's why it show the same value, if you want to show the different value for each row you should store it in array and at cellForRowAtIndexPath get it like you are doing for lbl1

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

    for lbl5 Your code cell code should be like this

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

    For the lbl 3 & lbl 5 when i execute this code with static value to get interest it only stores one value in the array

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

    to store every value you calculated in array you should use append

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

    as there is only 1 value in the array for every index path it gives 0th value in the array as per your modulo operation so it shows same value in each row

    0 讨论(0)
  • 2021-01-26 06:49

    The main issue is with your data array.

    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.

    What you should do instead:

    data.append(interest)
    

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

    0 讨论(0)
提交回复
热议问题