Théo Fleismaher Théo Fleismaher - 4 months ago 19
iOS Question

Ranking numbers' loop in swift

I am trying to make a ranking in a

tableView
in Swift but I can't set the numbers of each cell. I am trying to make a
for loop
to start with the number 1 and increment it as long as I have cells in my
tableView
.

I've tried this :

@IBOutlet weak var rank: UILabel!

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


A
Use of unresolved identifier numberOfRows
error appears. I've tried to replace it by
myCell.count
or other
elements.count
but it doesn't work either.

I think it's pretty easy to do but I'm new to Swift and I don't manage to do it.

Any help would be appreciated.

Here is the full code (the rank's IBOutlet is on another page) :

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
}

}

Answer

You don't need any loop. Just use the indexPath.row. It's already in the order from 0 to arrDataArticles.count - 1

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

For full context:

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
}