感谢您将来的帮助!那是我的阵列:
var malls: [Mall] = [
Mall(name:"yagey", distance:""),
Mall[name:"tigey", distance:"")
]
如何通过mall.distance对商场进行排序? 这是我的tableView
的tableView
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! EateriesTableViewCell
let mall = mallToDisplayAt(indexPath: indexPath)
let mallLocate = CLLocation(latitude: mall.latitude, longitude: mall.longitude)
print (mallLocate)
let distanceInMeters = mallLocate.distance(from: coords) / 1000
let distanceInMetersString = String(format: "%.1f", distanceInMeters)
for index in 0..<malls.count {
var mall = malls[index] // copy a mall value
mall.distance = distanceInMetersString // change the copy of mall
malls[index] = mall // put the updated mall back into the array
}
cell.distanceLabel.text = distanceInMetersString + "км"
malls.sort() { $0.distance < $1.distance }
print (mall.distance)
return cell
}
答案 0 :(得分:1)
首先对商场进行排序,然后将其用作TableView的数据源,TableView将显示按照数组排序商场中的顺序排序的单元格:
let sortedMalls = malls.sorted { (mallOne, mallTwo) -> Bool in
return mallOne.distance < mallTwo.distance
}
也是一个班轮:
let sortedMalls = malls.sorted { $0.mallOne.distance < $1.mallTwo.distance }
使用sortedMalls:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell