排名数字'快速循环

时间:2016-08-05 09:16:18

标签: ios swift loops ranking

我正在尝试在Swift的tableView中进行排名,但我无法设置每个单元格的数量。我试图让for loop从数字1开始,只要我的tableView中有单元格,就增加它。

我试过这个:

@IBOutlet weak var rank: UILabel!

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

出现Use of unresolved identifier numberOfRows错误。我已尝试将其替换为myCell.count或其他elements.count,但它也无效。

我认为这很容易做到,但我是Swift的新手并且我没有设法做到这一点。

任何帮助都将不胜感激。

以下是完整代码(排名的IBOutlet在另一页上):

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
}

}

1 个答案:

答案 0 :(得分:2)

你不需要任何循环。只需使用indexPath.row即可。它已经从0到arrDataArticles.count - 1

的顺序
myCell.rank.text = "\(indexPath.row)"

完整背景:

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
}