【问题标题】:TableViewController cell not populating dataTableViewController 单元格不填充数据
【发布时间】:2019-08-09 10:28:52
【问题描述】:

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

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
}



}

【问题讨论】:

  • 请不要发布一大堆代码,然后简单地说“为什么这不起作用”。只发布相关代码。做一些调试。缩小问题范围。清楚地解释什么不起作用以及在哪里。清楚地解释您需要帮助的代码。
  • 之所以把代码全部贴出来是为了方便调试。
  • 不要认为这里有足够的内容(没有描述)来准确地确定您正在尝试做什么(例如,屏幕如何相互连接)。它看起来就好像您正在向 MapViewController 中的数组 (poi[]) 添加点。 TableViewController 中的数据源也称为poi[],但这是一个不同的数组。我看不出你是如何同步这两者的——也许这就是问题所在?你明白它们是不同的(尽管它们的名字相同)?
  • @user2957494 在发布之前进行调试是您的工作。

标签: ios swift uitableview cocoa-touch


【解决方案1】:

你可能需要

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

并确保 segue 源已连接到 vc 本身(从 IB 内 mapVC 的黄色 roundec 图标拖动它)

【讨论】:

    【解决方案2】:

    当我运行代码时,点击地图并没有将任何对象添加到 pointOfInterest,您正在 loadPointOfInterests 上进行循环以将对象添加到 poi 但它是空的,所以请确保在poi中添加一个Place对象

    在VisitationTableViewController,确保添加

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

    【讨论】:

    • 我向 poi 添加了一个地点对象,瞧,它起作用了。谢谢 Abdalrahim。
    猜你喜欢
    • 2014-10-14
    • 2011-11-08
    • 1970-01-01
    • 2020-10-21
    • 2015-06-04
    • 1970-01-01
    • 2017-08-16
    • 1970-01-01
    • 2023-01-13
    相关资源
    最近更新 更多