2016-03-21 5 views
-2

Ich möchte meine Zahlen in die uitableviewcell Schleife und Drucken aller Werte von der höchsten Zahl bis zum niedrigsten und druckt sie in jeder Zelle. Ich habe den vollständigen Code veröffentlicht. Siehe den cell.text Ausgang. Dies ist mein Code:Schleife verringerte Zahlen in 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! 
    }  
} 

Dieser Code gibt mir immer die letzte Nummer der Schleife für alle Werte, ich mag es ändern, um vom ersten Wert auf den letzten in jeder Zelle zu schreiben.

+1

Sie können es nicht so machen. Die Tabellenansicht ruft "cellForRowAtIndexPath" so auf, wie sie es für richtig hält, basierend darauf, welche Zeilen auf dem Bildschirm angezeigt werden. Sie könnten Ihre Berechnungen in einem Array speichern und den Inhalt in 'cellForRowAtIndexPath' anzeigen. – Michael

+1

Was ist der Sinn dieser 'if' Aussage? "initial" wird immer gleich sein. – rmaddy

Antwort

3

Willkommen bei SO. Weder deine Frage noch dein Code machen Sinn.

Es sieht für mich aus, als ob Sie keine Ahnung haben, wie Sie Tabellenansichten verwenden.

Sie müssen ein Datenmodell (normalerweise ein Array) einrichten, das die Daten für Ihre Tabellenansicht enthält. Wenn Sie dann die cellForRowAtIndexPath-Methode aufrufen, sehen Sie sich die Zeile (oder Zeile und Sektion) in der Anfrage an, holen Sie die Daten für diese Zeile (oder Zeile & Sektion für eine unterteilte Tabellenansicht) und verwenden Sie diese Daten, um eine Zelle zu konfigurieren Rückkehr.

0

es auf diese Weise versuchen:

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   
} 

Sie müssen verstehen, dass cellForRow:atIndexPath eine UITableView Datenquelle Methode ist, die durch den UIKit Rahmen genannt wird. Es wird aufgerufen, wenn die Tabellenansicht geladen wird, basierend auf der Anzahl der Abschnitte (angegeben durch eine andere Datenquellenmethode) und der Anzahl der Zeilen in jedem Abschnitt (auch von einer anderen Datenquellenmethode angegeben).

+0

'initial - indexPath.row' ist überhaupt nicht das, was das OP in jeder Zelle will. – rmaddy

+0

Seine Frage lautet: "Drucke alle Werte von der höchsten bis zur niedrigsten Zahl" - ist das nicht das, was das tut? –

+0

Sehen Sie sich den Code an, der vom OP gesendet wurde. Ich denke, jede Zeile würde mit dem anfänglichen Kontostand beginnen und dann um die monatliche Zahlung verringern. Das OP ist nicht zu klar, aber es ist sicherlich nicht gemeint, dekrementieren Sie jede Zeile um 1, wie in Ihrer Antwort gezeigt. – rmaddy

0

So funktioniert die Tabellenansicht, wie Duncan gesagt hat, müssen Sie wissen.

/*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 
    } 

} 
+0

loop = Array () dann „selbst hinzufügen. data = [Anfänglich] "in der Schleife cell.lbl5.text = "\ (data [indexPath.row])" diese Methode wird mir error "fatal error: Array-Index außerhalb des Bereichs" –