ios - UItableview 行未在 swift4 中使用 Mapkit 显示

标签 ios swift uitableview mapkit

我正在尝试在 View Controller 上拥有一个 map 套件和一个工具。我有使用注释的 map 套件,但我想在 map 套件下方的桌面上显示名称。

发生的情况是“tableView(_ tableView: UITableView, cellForRowAt indexPath:”甚至没有被注意到。我尝试添加“self.tableView.reloadData()”但仍然没有。

有人可以告诉我我哪里出错了吗?

import Foundation
import UIKit
import CoreLocation
import MapKit
class customPin: NSObject, MKAnnotation {
    var coordinate: CLLocationCoordinate2D
    var title: String?
    var subtitle: String?

    init(pinTitle:String, pinSubTitle:String, location:CLLocationCoordinate2D) {
        self.title = pinTitle
        self.subtitle = pinSubTitle
        self.coordinate = location
    }
}
class MainViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate,  UITableViewDelegate, UITableViewDataSource  {
    @IBOutlet weak var mapview: MKMapView!
 var tableView: UITableView = UITableView()

    var pipCan = [Int: pipiCanDBList]()
    let cellReuseIdentifier = "pipCans"
   override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
         pipiCanList()
         locationMap()
         }
     override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        tableView.frame = CGRect(x: 0, y: 450, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height)
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
        tableView.delegate = self
        tableView.dataSource = self
        self.view.addSubview(tableView)
    }
  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return pipCan.count
    }
 //HERE IS THE PROBLEM

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
     let cell:UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)

     print("test 123")
        // configure cell
      //  let point = pipCan[indexPath.row].
        cell.textLabel?.text = pipCan[indexPath.row]?.ParkName
       // cell.detailTextLabel?.text = "(\(point.coordinate.latitude), \(point.coordinate.longitude))"

        return cell
    }
     func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return "Section \(section )"
    }
 func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

   //get the json  information
    func pipiCanList(){

        let url = "http://blueXXXX.lk/Apps/pipiCan/Api/xxxx.php";
        let urlObj = URL(string:url);

        URLSession.shared.dataTask(with: urlObj!) {(data, response, error) in
            do{
                let pipiCans = try JSONDecoder().decode([pipiCanDBList].self, from: data!)

                for pipiCan in pipiCans{

                    let Latitude = (pipiCan.ParkLatitude! as NSString).doubleValue
                    let Longitude = (pipiCan.ParkLongitude! as NSString).doubleValue
                    let  location = CLLocationCoordinate2D(latitude: Latitude, longitude: Longitude)

                   DispatchQueue.main.async  {
                        let pin = customPin(pinTitle: pipiCan.ParkName!, pinSubTitle: pipiCan.Area!, location:  location)
                        self.mapview.addAnnotation(pin)
                    }

                }

            } catch{

                print ("Error - cannot get list")

            }

            }.resume()
         }
func locationMap(){

        let latitude1 = 41.3825
        let longitude1 = 2.17694

        let location = CLLocationCoordinate2D(latitude: latitude1  , longitude:longitude1 )
        let region = MKCoordinateRegion(center: location, span: MKCoordinateSpan(latitudeDelta: 0.05, longitudeDelta: 0.05))

        self.mapview.setRegion(region, animated: true)
        self.mapview.delegate = self
    }
 }

enter image description here

最佳答案

您在 pipiCanList 中遗漏了一些内容

self.pipCan = pipiCans
self.tableView.reloadData()

所以你的方法看起来像

func pipiCanList() {

    let url = "http://blueXXXX.lk/Apps/pipiCan/Api/xxxx.php";
    let urlObj = URL(string:url);

    URLSession.shared.dataTask(with: urlObj!) {(data, response, error) in
        do {
            let pipiCans = try JSONDecoder().decode([pipiCanDBList].self, from: data!)

            for pipiCan in pipiCans{

                let Latitude = (pipiCan.ParkLatitude! as NSString).doubleValue
                let Longitude = (pipiCan.ParkLongitude! as NSString).doubleValue
                let  location = CLLocationCoordinate2D(latitude: Latitude, longitude: Longitude)

                DispatchQueue.main.async  {
                    let pin = customPin(pinTitle: pipiCan.ParkName!, pinSubTitle: pipiCan.Area!, location:  location)
                    self.mapview.addAnnotation(pin)
                }
            }

            self.pipCan = pipiCans
            self.tableView.reloadData()

        } catch {
            print ("Error - cannot get list")
        }
        }.resume()
}

关于ios - UItableview 行未在 swift4 中使用 Mapkit 显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55647650/

相关文章:

iphone - 如何从 iOS 连接到 Web 服务(SOAP + DES)?

ios - 如何使用 UITableViews 通过 Swift 创建静态和动态行

objective-c - 订购特定格式数字的 NSString(例如 3 :23. 123)?

ios - 为 NSDataDetector 设置上下文日期

ios - 64 位 ios 模拟器中的 TableView 加载问题

ios - 整数乘以 double 并将结果存储为 double

swift - Actor 是否保证相同的执行线程?

swift - 有没有办法缩短其中包含 arc4random_uniform( ) 的 if 语句?

ios - 滚动时 UITableView 内容重置

ios - 使用 Swift 的带有 MVVM 的 UITableView