2016-08-05 19 views
0

Ich versuche, ein Ranking in einem tableView in Swift machen, aber ich kann nicht die Nummern jeder Zelle festlegen. Ich versuche ein for loop zu machen, um mit der Nummer 1 zu beginnen und es zu erhöhen, solange ich Zellen in meinem tableView habe.Ranking-Nummern 'Loop in Swift

Ich habe dies versucht:

@IBOutlet weak var rank: UILabel! 

for var i = 1; i <= numberOfRows; i += 1 { 
     myCell.name.text = "i" 
    } 

Ein Use of unresolved identifier numberOfRows Fehler angezeigt. Ich habe versucht, es durch myCell.count oder andere elements.count zu ersetzen, aber es funktioniert auch nicht.

Ich denke, es ist ziemlich einfach zu tun, aber ich bin neu in Swift und ich schaffe es nicht.

Jede Hilfe wäre willkommen.

Hier ist der vollständige Code (der Rang des IBOutlet auf einer anderen Seite ist):

import UIKit 
import CoreLocation 

class VC0: UIViewController, UITableViewDelegate, UITableViewDataSource, CLLocationManagerDelegate { 

let LocationManager = CLLocationManager() 

var arrDataArticles: NSMutableArray! 

@IBOutlet weak var myCity: UINavigationItem! 

@IBOutlet weak var myTableView: UITableView! 

var images = UIImage(named: "avatar") 

override func viewDidLoad() { 
    super.viewDidLoad() 

    //CoreLocation 
    self.LocationManager.delegate = self 
    self.LocationManager.desiredAccuracy = kCLLocationAccuracyBest 
    self.LocationManager.requestWhenInUseAuthorization() 
    self.LocationManager.startUpdatingLocation() 

    //Data 
    self.getAllArticles() 
} 

//Location 
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 

    CLGeocoder().reverseGeocodeLocation(manager.location!, completionHandler: { 
     (placemarks, error) -> Void in 

     if error != nil { 
      print("Error") 
     } 

     if let pm = placemarks?.first { 
      self.displayLocationInfo(pm) 
     } 
     else { 
      print("Error : data error") 
     } 

    }) 
} 

func displayLocationInfo (placemark: CLPlacemark) { 

    self.LocationManager.stopUpdatingLocation() 

    print(placemark.subThoroughfare) 
    print(placemark.thoroughfare) 
    print(placemark.locality) 
    print(placemark.postalCode) 
    print(placemark.subAdministrativeArea) 
    print(placemark.administrativeArea) 
    print(placemark.country) 
    myCity.title = placemark.locality 

} 

func locationManager(manager: CLLocationManager, didFailWithError error: NSError) { 
    print("Error :" + error.localizedDescription) 
} 

//Data 
func getAllArticles() { 
    arrDataArticles = NSMutableArray() 
    arrDataArticles = ModelBD.getInstance().getAllArticles() 
    myTableView.reloadData() 
} 

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return arrDataArticles.count 
} 

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

    let myCell: CustomCell = tableView.dequeueReusableCellWithIdentifier("cell") as! CustomCell 

    let article: ArticlesData = arrDataArticles.objectAtIndex(indexPath.row) as! ArticlesData 

    for var i = 1; i <= numberOfRows; i += 1 { 
     myCell.rank.text = "i" 
    } 

    //myCell.rank.text = ranks[indexPath.row] 
    myCell.photo.image = images 

    //myCell.name.text = "i" 
    myCell.city.text = article.districtArticle 

    return myCell 
} 

} 
+0

@NDoc ich nur den Rest des Codes hinzugefügt. – TheoF

+0

@Khuong danke für deinen Vorschlag, also denkst du, ich sollte eine Zählfunktion erstellen, anstatt eine Schleife zu machen? – TheoF

+1

möchte nicht die for-Schleife. Sie werden nur in cellforindexpath eingeben myCell.rank.text = indexpath.row wird wie ein Rang 0,1,2, bis Array-Anzahl angezeigt. –

Antwort

2

Sie keine Schleife benötigen. Verwenden Sie einfach die indexPath.row. Es ist bereits in der Reihenfolge von 0 zu arrDataArticles.count - 1

myCell.rank.text = "\(indexPath.row)" 

Für vollständigen Kontext:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

    let myCell: CustomCell = tableView.dequeueReusableCellWithIdentifier("cell") as! CustomCell 

    let article: ArticlesData = arrDataArticles.objectAtIndex(indexPath.row) as! ArticlesData 

    myCell.rank.text = "\(indexPath.row)" 

    //myCell.rank.text = ranks[indexPath.row] 
    myCell.photo.image = images 

    //myCell.name.text = "i" 
    myCell.city.text = article.districtArticle 

    return myCell 
} 
+0

Ich habe es bereits versucht, aber es heißt "Kann den Wert vom Typ 'Int' nicht zuweisen, um 'String' einzugeben. – TheoF

+0

siehe aktualisierte Antwort –

+0

Perfekt funktioniert, vielen Dank! – TheoF