swift 中的排名数字循环
Ranking numbers' loop in swift
我正在尝试在 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
}
}
您不需要任何循环。只需使用 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
}
我正在尝试在 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
}
}
您不需要任何循环。只需使用 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
}