TableViewController单元格未填充数据

时间:2019-03-18 23:57:49

标签: ios swift uitableview cocoa-touch

我不知道为什么表视图控制器没有填充placeMarks.namecoordinates。用户点击地图上的图钉,然后将位置添加到模型中。但是,它将数据发送到表视图控制器的位置并没有显示模型中的数据。下面是我的代码。

import Foundation
import CoreLocation


class PlaceList : NSObject  {

let locations: Places
var coordinate: CLLocationCoordinate2D { return locations.coordinate }

init(point: Places) {
    self.locations = point
    super.init()
}

var title: String? {
    return locations.name
}

var subtitle: String? {
    return "(\(locations.coordinate.latitude), \(locations.coordinate.longitude))"
}

}

import  CoreLocation
import Foundation

class Places: NSObject {



var name: String
var coordinate: CLLocationCoordinate2D

init(name: String, coordinate: CLLocationCoordinate2D) {
    self.name = name
    self.coordinate = coordinate
}

}

import UIKit
import MapKit
import CoreLocation

class MapViewController: UIViewController, MKMapViewDelegate,CLLocationManagerDelegate {

var placeModel: PlaceList?
var pointOfInterest: [Places] = []

var poi: [Places] = [] {
    didSet {
        pointOfInterest = poi

    }
}

//Creating mapview object
var mapView: MKMapView!
var geoCoder =  CLGeocoder()

//Initial cooridinate
var latitude = 43.136581
var longitude = -87.941101

var latitudeDelta = 0.3
var longitudeDelta = 0.3

//the object that determines the location
let placeManger = CLLocationManager()

//loading the map on the scene
override func loadView() {
    super.loadView()
    mapView = MKMapView()
    view = mapView
    placeManger.startUpdatingLocation()
}

override func viewWillAppear(_ animated: Bool) {

    super.viewWillAppear(true)

   navigationItem.title = "Travel Wishlist"
    navigationController?.navigationBar.prefersLargeTitles = true
    navigationController?.navigationBar.barTintColor = .blue

    placeManger.delegate = self
    placeManger.requestWhenInUseAuthorization()
    mapView.delegate = self

    navigationItem.rightBarButtonItem = rightBar
    navigationItem.leftBarButtonItem = leftBar

    mapView.addGestureRecognizer(tapGestrueRecongnizer)

    centerMapInInitialCoordinates()
    showPointsOfInterestInMap()


}

func showPointsOfInterestInMap() {
    mapView.removeAnnotations(mapView.annotations)

    for point in poi {
        let pin = PlaceList(point: point)
        mapView.addAnnotation(pin as! MKAnnotation)
    }
}

func centerMapInInitialCoordinates() {
    let span:MKCoordinateSpan = MKCoordinateSpan(latitudeDelta: latitudeDelta, longitudeDelta: longitudeDelta)
    let region:MKCoordinateRegion = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: latitude, longitude: longitude), span: span)
    mapView.setRegion(region, animated: true)

}

var tapGestrueRecongnizer: UITapGestureRecognizer {

    let tapGesture = UITapGestureRecognizer(target: self, action: #selector(loadPointOfInterests(recongnizer:)))
    return tapGesture

}

var rightBar: UIBarButtonItem  {

    let rightBarButton =
        UIBarButtonItem(title: "Find a place to add",
                        style: .plain, target: self,
                        action: #selector(rightbarButton))
    rightBarButton.tintColor = .white

    return rightBarButton

}

var leftBar: UIBarButtonItem  {

    let leftBarButton =
        UIBarButtonItem(title:
            "Look up", style: .plain,
                       target: self, action: #selector(leftButton))
    leftBarButton.tintColor = .white

    return leftBarButton

}

@objc func leftButton(leftButton: UIBarButtonItem) {

    print("Left BarButton")

    performSegue(withIdentifier: "place", sender: leftButton)
    addPlace()


}

@objc func rightbarButton(right: UIBarButtonItem) {

    addPlace()

}

func addPlace(){
   // mapView.removeAnnotations(mapView.annotations)

    for point in pointOfInterest {
        let pin = PlaceList(point: point)
        mapView.addAnnotation(pin as! MKAnnotation)

    }
    filterVisiblePOI()

}

func filterVisiblePOI() {
    let visibleAnnotations = self.mapView.annotations(in: self.mapView.visibleMapRect)
    var annotations = [PlaceList]()
    for visibleAnnotation in visibleAnnotations {
        if let annotation = visibleAnnotation as? PlaceList {
            annotations.append(annotation)
        }
    }

}


@objc func loadPointOfInterests(recongnizer:UITapGestureRecognizer) {

    let locationPoint = recongnizer.location(in: mapView)
    let cooridinate = mapView.convert(locationPoint, toCoordinateFrom: mapView)

    let annotation = MKPointAnnotation()
    annotation.coordinate = cooridinate

    mapView.addAnnotation(annotation)

    geoCoder.reverseGeocodeLocation(CLLocation(latitude: cooridinate.latitude, longitude: cooridinate.longitude)) {(placeMarks : [CLPlacemark]?, error: Error?) in
        if let placeLocation = placeMarks?[0] {

            if error == nil && (placeMarks?.count)! > 0 {

                let locationString = " \(placeLocation.name!)"
                annotation.coordinate = cooridinate
                annotation.title = locationString
                print(locationString)


            }
            for point in self.pointOfInterest {
                let pin = PlaceList(point: point)
                self.mapView.addAnnotation(pin as! MKAnnotation)
                self.poi.append(point)
                print(point)
                print(self.pointOfInterest)
                print(pin)


            }


        }

    }

}

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {

        self.navigationItem.rightBarButtonItem?.title = "Find a place to add"

}

private func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) {

  //To Do


    }

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if segue.identifier == "place" {
        let nav = segue.destination as! UINavigationController
        let detailViewControll = nav.topViewController as! VisitationTableViewController

        for point in pointOfInterest {
            let pin = PlaceList(point: point)
            mapView.addAnnotation(pin as! MKAnnotation)
            detailViewControll.poi = pointOfInterest



        }


    }


}

}

import Foundation
import UIKit

class VisitationTableViewController: UITableViewController {

var poi: [Places] = []



override func viewWillAppear(_ animated: Bool) {

     super.viewWillAppear(true)

    let dissButton = UIBarButtonItem(title: "Done", style: .done
        , target: self, action: #selector(dismisController))
    self.navigationItem.rightBarButtonItem = dissButton
    dissButton.tintColor = .white

    navigationController?.navigationBar.prefersLargeTitles = true
    navigationItem.title = "Visited"
    navigationController?.navigationBar.barTintColor = .blue
    print("view didload")
    print(poi)

}

@objc func dismisController() {

    presentingViewController?.dismiss(animated: true, completion: nil)

}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return poi.count
}

 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "locationCell", for: indexPath)

    let point = poi[indexPath.row]
    cell.textLabel?.text = point.name
    cell.detailTextLabel?.text = "(\(point.coordinate.latitude), \(point.coordinate.longitude))"

    return cell
}



}

2 个答案:

答案 0 :(得分:0)

您可能需要

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
  self.performSegue(withIdentifier:"place",sender:nil)
}

并确保segue源已连接到vc本身(将其从IB中mapVC的黄色roundec图标中拖动出来)

答案 1 :(得分:0)

当我运行代码时,点击地图并没有向pointOfInterest添加任何对象,您正在loadPointOfInterests上循环以向poi添加对象,但是为空,因此请确保在Place

中添加一个poi对象

VisitationTableViewController中,确保添加

override func viewDidLoad() {
    super.viewDidLoad()
    self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "locationCell")
}