2017-04-06 28 views
0

我是一個完整的初學者,並在互聯網上搜索解決方案。 我想按照距離向用戶排序地點,並在表格視圖中顯示名稱(按最近距離排序)。 我能夠排序另一個數組並顯示在表格視圖中,並且我可以通過距離對其他地方進行排序,這要感謝stackoverflow搜索。 但我無法將它放在一起,並在表格視圖中顯示這些地方的名稱。Swift 3,按屬性排序類並填充表視圖

我希望我描述的一切都足夠讓你理解我的問題。

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) 
} 

回答

0

您需要首先subscriptArray那麼就需要訪問它的屬性namefacilityoperatedlocation

應該

cell.textLabel?.text = places[indexPath.row].name 

代替

cell.textLabel?.text = places.name[indexPath.row] 

注:不要設置tableFooterViewcellForRowAt方法,而不是設置它的viewDidLoad內。

override func viewDidLoad() { 
    super.viewDidLoad() 
    tableView.tableFooterView = UIView(frame: .zero) 
} 
+0

非常感謝您的幫助!關於tableFooterView,如果我將它設置在viewDidLoad「成員'tableView(_:numberOfRowsInSection :)」的模糊引用中,它會收到以下錯誤消息「 – teamoh