我想將我的數字循環到uitableviewcell中,並將所有值從最高編號打印到最低值,並將它們打印在每個單元格中。我發佈了完整的代碼。請參閱cell.text
輸出。這是我的代碼:循環在uitableviewcell中減少的數字
import UIKit
class tableViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet var tableView: UITableView!
var arr = [Int]()
var cell:tableCell!
var Payment: float_t! = 59600
var years1: float_t! = //15 * 12 = 180
var monthlyPayment: float_t! = 471
var interest: float_t! = 5%
var principal: float_t! = 222
var interstate: float_t! = 249
var initil: float_t!
var data = Array<float_t>()
var data2: NSMutableArray = []
override func viewDidLoad() {
super.viewDidLoad()
let c = Int(years1)
arr += 0...c
tableCalculation()
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)")
self.data2 = [initil]
}
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arr.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
cell = self.tableView.dequeueReusableCellWithIdentifier("cell") as! tableCell
cell.lbl1.text = "\(arr[indexPath.row])"
cell.lbl2.text = currencyFormatter(monthlyPayment)
cell.lbl3.text = currencyFormatter(interest)
cell.lbl4.text = currencyFormatter(principil)
cell.lbl5.text = "\(self.data2[indexPath.row % data2.count])"
return cell
}
// 4
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
print("Row \(indexPath.row) selected")
}
// 5
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 70
}
func currencyFormatter(Price: float_t) ->String {
let currencyFormatter = NSNumberFormatter()
currencyFormatter.usesGroupingSeparator = true
currencyFormatter.numberStyle = NSNumberFormatterStyle.DecimalStyle
// localize to your grouping and decimal separator
let numberOfPlaces: float_t = 1.0
let multiplier: float_t = pow(10.0, numberOfPlaces)
let num = Price
let rounded = round(num * multiplier)/multiplier
let priceString = currencyFormatter.stringFromNumber(rounded)
return priceString!
}
}
此代碼總是給我的環路的所有值的最後一個數字,我想改變它從第一值寫入到最後一個的每一個細胞。
你不能那樣做。根據屏幕上哪些行可見,表視圖根據它所選的順序調用'cellForRowAtIndexPath'。您可以將計算存儲在數組中,並將內容顯示在「cellForRowAtIndexPath」中。 – Michael
'if'語句有什麼意義? '初始'將永遠等於自己。 – rmaddy