class TransferStepDetailsViewController: UIViewController {
@IBOutlet weak var timeLabel: UILabel!
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var btn: UIButton!
var tran : AMapTransit?
var route: AMapRoute?
var _startPoi = BMKPoiInfo()
var _endPoi = BMKPoiInfo()
var list = [AnyObject]()
override func viewDidLoad() {
super.viewDidLoad()
let str = String(format: "%.1f", CGFloat(tran?.distance ?? 0)/CGFloat(1000))
let time = Date.timeStampToString(timeStamp: tran?.duration ?? 0).replacingOccurrences(of: ":", with: "小时")
timeLabel.text = "\(time)分钟(\(str)公里)"
tableView.register(UINib(nibName: "TransferStepTableViewCell", bundle: nil), forCellReuseIdentifier: "TransferStepCell")
tableView.register(UINib(nibName: "TransferStepSubTableViewCell", bundle: nil), forCellReuseIdentifier: "TransferStepSubCell")
self.title = "换乘信息"
self.btn.layer.cornerRadius = 5
self.btn.layer.masksToBounds = true
setData()
}
func setData() {
let model = TransferStepDetailsModel()
model.title = "出发"
model.icon = "stationStart"
model.isWalk = true
list.append(model)
for seg in tran?.segments ?? [] {
let walk = seg.walking
if (walk != nil) {
//添加 步行
let model = TransferStepDetailsModel()
model.title = "步行\(walk?.distance ?? 0)米"
model.icon = "icon_person"
model.isWalk = true
list.append(model)
}
let oLine = seg.buslines
if (oLine != nil) {
let model = TransferStepDetailsModel()
var tag = 1
for line in oLine ?? []{
model.title = line.name
model.icon = "bus_icon"
model.isOpen = false
model.tag = tag
var busLine = [TransferLinesModel]()
if (line.departureStop != nil) {
let m = TransferLinesModel()
m.title = line.departureStop.name
m.icon = "icon_busstation_gray"
m.tag = tag
busLine.append(m)
}
for li in line.viaBusStops ?? [] {
let m = TransferLinesModel()
m.title = li.name
m.icon = "icon_busstation_gray"
m.tag = tag
busLine.append(m)
}
if (line.arrivalStop != nil) {
let m = TransferLinesModel()
m.title = line.arrivalStop.name
m.icon = "icon_busstation_gray"
m.tag = tag
busLine.append(m)
}
model.subline = busLine
tag += 1
}
if (oLine ?? []).count > 0 {
list.append(model)
}
}
}
let endModel = TransferStepDetailsModel()
endModel.title = "结束"
endModel.icon = "stationEnd"
endModel.isWalk = true
list.append(endModel)
self.tableView.reloadData()
}
//地图详情
@IBAction func mapDetailsAction(_ sender: Any) {
let channel = TransferStepMapViewController(nibName: "TransferStepMapViewController", bundle: nil)
channel.title = "地图详情"
channel.tran = tran
channel.startCoordinate = CLLocationCoordinate2DMake(CGFloat(_startPoi.pt.latitude), CGFloat(_startPoi.pt.longitude))
channel.destinationCoordinate = CLLocationCoordinate2DMake(CGFloat(_endPoi.pt.latitude), CGFloat(_endPoi.pt.longitude))
self.navigationController?.pushViewController(channel, animated: true)
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
}
*/
}
extension TransferStepDetailsViewController:UITableViewDelegate,UITableViewDataSource{
func numberOfSections(in tableView: UITableView) -> Int {
return list.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let obj = list[section] as! TransferStepDetailsModel
return (obj.isOpen ?? false) == true ? (obj.subline?.count ?? 0) : 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let obj = list[indexPath.section] as! TransferStepDetailsModel
let m = (obj.subline ?? [])[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: "TransferStepSubCell") as! TransferStepSubTableViewCell
cell.imageV.image = UIImage(named: m.icon ?? "")
cell.titleLabel.text = m.title ?? ""
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 30
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 50
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let views = Bundle.main.loadNibNamed("TranferHeaderView", owner: nil, options: nil)?.first as! TranferHeaderView
views.frame = CGRect(x: 0, y: 0, width: kScreenWidth, height: 50)
let obj = list[section]
if obj.isKind(of: TransferStepDetailsModel.classForCoder()) {
let m = obj as! TransferStepDetailsModel
views.imageV.image = UIImage(named: m.icon ?? "")
views.contentLab.text = m.title ?? ""
views.rowBtn.isHidden = (m.isWalk ?? false)
if !(m.isWalk ?? false) {
views.rowBtn.setTitle("\(m.subline?.count ?? 0) 站", for: .normal)
}
views.block = {()->Void in
var m = obj as! TransferStepDetailsModel
m.isOpen = !(m.isOpen ?? false)
self.list[section] = m
self.tableView.reloadData()
}
}
return views
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 0.1
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
}
}
class TransferStepDetailsModel:NSObject {
var title:String?
var icon:String?
var isOpen:Bool?
var isWalk:Bool?
var subline:[TransferLinesModel]?
var tag:NSInteger?
}
class TransferLinesModel:NSObject {
var title:String?
var icon:String?
var tag:NSInteger?
}
网友评论