所以我有一个导航控制器,它包含一个视图控制器和一个UITableViewController。视图控制器包含mapview和textfield,tableviewcontroller搜索位置。
当我在表格视图控制器中选择一个位置时,我希望能够将该地址存储在文本字段中。我可以得到我想要打印的地址
然而,每当我尝试将该地址分配给文本字段时,应用程序崩溃时出现“致命错误:在展开可选值时意外发现nil”有人知道原因吗?这是我的tableview控制器代码:
import UIKit
import MapKit
class LocationSearchTable: UITableViewController {
weak var handleMapSearchDelegate: HandleMapSearch?
var matchingItems: [MKMapItem] = []
var mapView: MKMapView?
func parseAddress(_ selectedItem:MKPlacemark) -> String {
// put a space between "4" and "Melrose Place"
let firstSpace = (selectedItem.subThoroughfare != nil &&
selectedItem.thoroughfare != nil) ? " " : ""
// put a comma between street and city/state
let comma = (selectedItem.subThoroughfare != nil || selectedItem.thoroughfare != nil) &&
(selectedItem.subAdministrativeArea != nil || selectedItem.administrativeArea != nil) ? ", " : ""
// put a space between "Washington" and "DC"
let secondSpace = (selectedItem.subAdministrativeArea != nil &&
selectedItem.administrativeArea != nil) ? " " : ""
let addressLine = String(
format:"%@%@%@%@%@%@%@",
// street number
selectedItem.subThoroughfare ?? "",
firstSpace,
// street name
selectedItem.thoroughfare ?? "",
comma,
// city
selectedItem.locality ?? "",
secondSpace,
// state
selectedItem.administrativeArea ?? ""
)
return addressLine
}
}
extension LocationSearchTable : UISearchResultsUpdating {
func updateSearchResults(for searchController: UISearchController) {
guard let mapView = mapView,
let searchBarText = searchController.searchBar.text else { return }
let request = MKLocalSearchRequest()
request.naturalLanguageQuery = searchBarText
request.region = mapView.region
let search = MKLocalSearch(request: request)
search.start { response, _ in
guard let response = response else {
return
}
self.matchingItems = response.mapItems
self.tableView.reloadData()
}
}
}
extension LocationSearchTable {
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return matchingItems.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell")!
let selectedItem = matchingItems[indexPath.row].placemark
cell.textLabel?.text = selectedItem.name
cell.detailTextLabel?.text = parseAddress(selectedItem)
// let right = (cell.detailTextLabel?.text)!
// print(right)
//print(selectedItem.name)
// print(parseAddress(selectedItem))
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let selectedItem = matchingItems[indexPath.row].placemark
handleMapSearchDelegate?.dropPinZoomIn(selectedItem)
dismiss(animated: true, completion: nil)
let cell = tableView.dequeueReusableCell(withIdentifier: "cell")!
cell.detailTextLabel?.text = parseAddress(selectedItem)
DispatchQueue.main.async {
let right = cell.detailTextLabel?.text
print (right!)
let vc = ViewController()
vc.testTxt.text = right!
}
}
}
答案 0 :(得分:0)
您不应该在cell = tableView.dequeueReusableCell(withIdentifier: "cell")!
方法中执行此操作didSelectRow
。如果你想看看哪个项目被选中,你需要通过indexPath.row
从数组中获取它作为这样的索引
verride func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let selectedItem = matchingItems[indexPath.row].placemark
}
要将此数据传递给上一个视图控制器,您需要一个展开segue like this post。
答案 1 :(得分:0)
在<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:theme="@style/ThemeOverlay.AppCompat.Dark"
app:titleTextColor="@android:color/white"
app:contentInsetStartWithNavigation="0dp">
</android.support.v7.widget.Toolbar>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:text="Price:"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center|bottom"
android:textStyle="bold"
android:textColor="@color/colorPrimary"
android:paddingLeft="2dp"
android:layout_margin="5dp"/>
<TextView
android:text="block"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center|bottom"
android:textStyle="bold"
android:textColor="@color/colorPrimary"
android:paddingLeft="2dp"
android:layout_margin="5dp"/>
<TextView
android:text="floor"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center|bottom"
android:textStyle="bold"
android:textColor="@color/colorPrimary"
android:paddingLeft="2dp"
android:layout_margin="5dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="5">
<Button
android:id="@+id/button01"
android:text="button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:layout_gravity="bottom"
android:textColor="@android:color/white"
/>
</LinearLayout>
</LinearLayout>
的扩展程序中,您正在解除控制器,然后尝试将单元格出列。由于控制器不再存在,我认为tableView为零,didSelectRow
导致崩溃。您可能想要做的是dequeueing
pushing
您应该在当前TableViewController
的引用中传递,然后使用它来设置controller
属性
textfield