UItableview行未在swift4中显示mapkit

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UItableview行未在swift4中显示mapkit相关的知识,希望对你有一定的参考价值。

我试图在viewcontroller上有一个mapkit和一个uitable。我有mapkit处理annoitations,但我想在mapkit下面显示名称。

发生的事情是'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()
}

以上是关于UItableview行未在swift4中显示mapkit的主要内容,如果未能解决你的问题,请参考以下文章

行未在数据网格 C# 中删除

flex-box 中的行换行未在 Safari 中换行

未在 UITableView Swift 4 的主要滑动操作中显示标题

数据较少时未在 UITableview 中调用 scrollViewDidEndDragging

为啥视图行未显示在 item.xml 中

列表视图中的行未显示