Uitableview Swift 3,按属性排序类并填充表视图
我是一个完全的初学者,在互联网上寻找解决方案。 我想按照与用户的距离对位置进行排序,并在表视图中显示名称(按照最接近的距离排序)。 由于stackoverflow搜索,我可以对另一个数组进行排序并在表视图中显示,还可以按距离对其他位置进行排序。 但我无法将其放在一起,并在表视图中显示这些位置的名称 我希望我描述的一切都足够好,让你理解我的问题Uitableview Swift 3,按属性排序类并填充表视图,uitableview,sorting,swift3,Uitableview,Sorting,Swift3,我是一个完全的初学者,在互联网上寻找解决方案。 我想按照与用户的距离对位置进行排序,并在表视图中显示名称(按照最接近的距离排序)。 由于stackoverflow搜索,我可以对另一个数组进行排序并在表视图中显示,还可以按距离对其他位置进行排序。 但我无法将其放在一起,并在表视图中显示这些位置的名称 我希望我描述的一切都足够好,让你理解我的问题 import UIKit import CoreLocation class Places { let name: String let facili
import UIKit
import CoreLocation
class Places {
let name: String
let facility: String
let operated: String
let location: CLLocation
init(name: String, facility: String, operated: String, location: CLLocation){
self.name = name
self.facility = facility
self.operated = operated
self.location = location
}
}
let place1 = Places(name: "Name1", facility: "Hospital", operated: "privat", location: CLLocation(latitude: 12.953761, longitude: 100.906278))
let place2 = Places(name: "Name2", facility: "Hospital", operated: "privat", location: CLLocation(latitude: 12.944278, longitude: 100.887897))
let place3 = Places(name: "Name3", facility: "Hospital", operated: "privat", location: CLLocation(latitude: 12.935769, longitude: 100.886871))
let place4 = Places(name: "Name4", facility: "Hospital", operated: "sozial", location: CLLocation(latitude: 12.927889, longitude: 100.884304))
let place5 = Places(name: "Name5", facility: "Hospital", operated: "sozial", location: CLLocation(latitude: 12.966825, longitude: 100.906108))
var places: [Places] = [place1, place2, place3, place4, place5]
var locationManager = CLLocationManager()
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, CLLocationManagerDelegate{
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return(places.count)
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "cell")
//Here comes the error
cell.textLabel?.text = places.name[indexPath.row]
tableView.tableFooterView = UIView(frame: .zero)
return(cell)
}
您首先需要使用
数组下标,然后需要访问其属性名称
,设施
,操作
和位置
应该是
cell.textLabel?.text = places[indexPath.row].name
而不是
cell.textLabel?.text = places.name[indexPath.row]
注意:不要在cellForRowAt
方法中设置tableFooterView
,而不要在viewDidLoad
中设置它
override func viewDidLoad() {
super.viewDidLoad()
tableView.tableFooterView = UIView(frame: .zero)
}
非常感谢你的帮助!!!关于tableFooterView,如果我在viewDidLoad中设置它,我会收到以下错误消息“对成员的tableView(uU2;:numberOfRowsInSection:)的不明确引用”