gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 05:51:18 25 4
gpt4 key购买 nike

我正在尝试在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com