Swift 4, Xcode I am trying to update my iOS application using Swift 4. It is a loan calculator that lists: 1. Monthly payment 2. Total interest 3. Total tax 4. Total amount My application works fine but I am trying to update it with a payout structure using a computer table view. I want it to show the: 1. Principal of every payment. 2. Interest of every payment. 3. Balance after every payment. How do I?: 1. Clear the whole table view. 2. Write a "for loop" getting it to use balance from the previous payment. 3. Insert the new row at the bottom of the table instead of index 0
Replies
Formatted for you, to make it readable.
loan calculator that lists:
1. Monthly payment
2. Total interest
3. Total tax
4. Total amount
My application works fine but I am trying to update it with a payout structure using a computer table view.
I want it to show the:
1. Principal of every payment.
2. Interest of every payment.
3. Balance after every payment.
How do I?:
1. Clear the whole table view.
2. Write a "for loop" getting it to use balance from the previous payment.
3. Insert the new row at the bottom of the table instead of index 0
That's not clear.
does the table view already exist ?
if yes, what is the content of cells ?
Why do you need to clear ?
If you clear, the bottom will be index 0 !
How many cells do you add at the end ?
I assume that you know how to calculate the different values you want to display.
To clear tableView:
remove all elements from data source
reload tableView
The loop should populate the data source (array of data), then call reload data
Yes, I made functions to calculate the values. I need to clear the table view every time I click the calculate button so it doesn't append to the current data.
You did not reply to my questions:
does the table view already exist ?
if yes, what is the content of cells ?
Why do you need to clear ?
If you clear, the bottom will be index 0 !
How many cells do you add at the end ?
I am sorry.
Yes, the table view already exists. I connectected it to my code and named it "table" and I set the cell identifier to "cell". The 2 required functions are set as:
var data: [String] = []
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell")!
cell.textLabel?.text = data[indexPath.row]
return cell
}
I need to clear the table view every time I click the calculate button so the new information doesn't get added to the previous data.
I figured out how to do that:
data = [""] table.reloadData()
I want the new row to get inserted after the previous added row. For example, I am going to make a payout structure for my loan calculator and I want the table to view as:
1. Principal: $435.23, Interest: $65.17, Balance: $5,323.65
2. Principal: $442.87, Interest: $57.52, Balance: $4,888.42
3. Principal: $450.01, Interest: $50.39, Balance: $4,445.54
Instead, by inserting the row at index 0, the data in the table is upside down.
3. Principal: $450.01, Interest: $50.39, Balance: $4,445.54
2. Principal: $442.87, Interest: $57.52, Balance: $4,888.42
1. Principal: $435.23, Interest: $65.17, Balance: $5,323.65
The cells are going to be as many months as the loan is:
data.count
I know how to format it all to currency. I am just having trouble with table views. I know that everything in iOS is comprised of table views. I have watched a lot of tutorials and googled about table views and I still have no luck.
Thank you for your help.
So, you clear the content of cells, you do not remove cells.
If I understand well, all you have to do is insert in data.
Use insert, such as
var data = ["1", "2", "3"]
data.insert("4", at: 0)
Note: I still do not understand why tou clear the cells, if you want to get this:
3. Principal: $450.01, Interest: $50.39, Balance: $4,445.54
2. Principal: $442.87, Interest: $57.52, Balance: $4,888.42
1. Principal: $435.23, Interest: $65.17, Balance: $5,323.65
Because if I do not clear the data before it inserts the new data then it won't start over with 1. It adds on to the previous data.