循环减少了uitableviewcell中的数字

时间:2016-03-21 22:48:08

标签: ios swift loops uitableview

我想将我的数字循环到uitableviewcell并将所有值从最高数字打印到最低数字并在每个单元格中打印它们。我发布了完整的代码。请参阅cell.text输出。这是我的代码:

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

这段代码总是给我所有值的循环的最后一个数字,我想把它改成从每个单元格中的第一个值写到最后一个。

3 个答案:

答案 0 :(得分:3)

欢迎来到SO。你的问题和你的代码都没有任何意义。

在我看来,你不知道如何使用表格视图。

您需要设置一个数据模型(通常是一个数组)来保存表视图的数据。然后,当调用您的cellForRowAtIndexPath方法时,查看请求中的行(或行和部分),获取该行的数据(或分段表视图的行和部分)并使用该数据配置一个单元格你回来了。

答案 1 :(得分:0)

以这种方式尝试:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {        
    cell = self.tableView.dequeueReusableCellWithIdentifier("cell") as! tableCell        
    cell.lbl5.text = "\(initial - indexPath.row*225)" //subtracts each consecutive row by a higher multiple of 225.       
    return cell        
}

您需要了解cellForRow:atIndexPath是由UIKit框架调用的UITableView数据源方法。根据节的数量(由另一个数据源方法指定)和每个节中的行数(也由另一个数据源方法指定)加载tableview时调用它。

答案 2 :(得分:0)

这是表格视图的工作原理,就像Duncan说你需要知道的那样。

/*We need to subclass UITableViewDataSource and UITableViewDelegate 
  so we gain access to tableView methods. Don't forget to link your 
  tableView in the storyboard to the delegate and datasource!*/

class controller: UIViewController, UITableViewDataSource, UITableViewDelegate {

    //1: Model. All the data you're gonna use for your table view
    var data = Array<String>()

    override func viewDidLoad() {
        //1: Populate model with some data when the view loads.
        self.data = ["Oranges", "Apples", "Bananas", "Grapes"]
    }

    //2: Number of rows in table view. This determines how many times #3 is called (below).
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        //We want the table view to have all the items in variable `data`. So, we need to have as many cells as the number of items in `data`
        return data.count
    }


    //3: This is called for each cell. It lets you customise each cell how you want. 
    //For us, we'll make the text inside the cell the current item in the `data` array
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        //Get the cell from the tableView. Use the identifier you've specified in the storyboard.
        let cell = tableView.dequeueReusableCellWithIdentifier("myCellIdentifier", forIndexPath: indexPath)
        //Set the text as the current item in `data`
        cell.textLabel?.text = data[indexPath.row]
        //Use this cell we've just created for the current row
        return cell
    }

}