2017-04-06 16 views
0

私は完全な初心者であり、解決策をインターネットで検索しました。 私は、ユーザーとの距離で場所を並べ替え、名前を(最も近い距離に最も近い順に並べ替えて)表形式で表示したいと思います。 別の配列をソートしてテーブルビューで表示できました。スタックオーバーフロー検索のおかげで、他の場所を距離で並べ替えることができました。 しかし、私はそれをまとめてテーブルビューにこれらの場所の名前を表示することはできません。Swift 3、プロパティ別のクラスをソートして表を表示する

私は、あなたが私の問題を理解するのに十分なすべてを記述したかったと思います。あなたのArrayで最初subscriptに必要

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

は、その性質namefacilityoperatedlocationにアクセスする必要があります。tableFooterView方法cellForRowAtではなく、それはviewDidLoad内側に設定を設定しないでください:

それは注意が

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

代わりの

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

でなければなりません。

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

ありがとうございました! tableFooterViewに関して、私がviewDidLoadの中で設定すると、次のエラーメッセージを受け取ります。 "メンバーへのあいまいな参照" tableView(_:numberOfRowsInSection :) " – teamoh

関連する問題