【问题标题】:TableView datasource value updation issueTableView 数据源值更新问题
【发布时间】:2018-06-28 10:56:40
【问题描述】:

我有一个应用程序,用户可以在其中从搜索栏中搜索位置。当用户搜索任何位置时,结果会显示在搜索栏下方的表格视图中。问题来了,当我搜索任何位置时,它会给出结果,但不会显示在表格视图中。我检查了委托和数据源,它们已正确连接,但表格视图仍然没有显示任何数据。我怎样才能在表格视图中获取该数据。我已经使用了断点,它也没有捕捉到断点。这是我如何获取搜索结果的代码,

extension LocationSearchTable : UISearchResultsUpdating {
func updateSearchResults(for searchController: UISearchController) {

    guard let mapView = mapView,
        let searchBarText = searchController.searchBar.text else { return }
    print(searchBarText)
    let request = MKLocalSearchRequest()
    request.naturalLanguageQuery = searchBarText
    request.region = mapView.region
    let search = MKLocalSearch(request: request)
    print(search)
    search.start { response, _ in
        guard let response = response else {
            return
        }
        self.matchingItems = response.mapItems
        print(self.matchingItems)
        self.tableView.reloadData()
    }
}

这里我将搜索结果传递给表格视图,

extension LocationSearchTable {

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return matchingItems.count
}

 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell")!
    let selectedItem = matchingItems[indexPath.row].placemark
    cell.textLabel?.text = selectedItem.name
    print(  cell.textLabel?.text)
    cell.detailTextLabel?.text = parseAddress(selectedItem: selectedItem)
    print( cell.detailTextLabel?.text)
    return cell
}

} 这是我搜索任何位置时的响应,

[<MKMapItem: 0x1c434d5d0> {
isCurrentLocation = 0;
name = California;
placemark = "California, California, United States @ <+37.13374180,-120.28640480> +/- 0.00m, region CLCircularRegion (identifier:'<+37.41896824,-119.30660700> radius 706259.58', center:<+37.41896824,-119.30660700>, radius:706259.58m)";
timeZone = "America/Los_Angeles (GMT-7) offset -25200 (Daylight)";

}]

这是我的 swift 文件的全部代码,

// weak var delegate: 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
}

【问题讨论】:

  • 在主线程中做self.matchingItems = response.mapItems; self.tableView.reloadData()
  • 让我试试@Larme
  • 仍然没有显示结果。 @Larme
  • 你用的是哪个swift版本,也可以包含完整代码
  • 我用的是swift4.1版本。并检查它我已经更新了我的代码。 @Sh_Khan

标签: swift tableview


【解决方案1】:

需要在viewDidLoad中设置delegate和dataSource

tableView.delegate = self
tableView.dataSource = self   

并像这样正确实现方法

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {t {
    return matchingItems.count
}

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
    print(  cell.textLabel?.text)
    cell.detailTextLabel?.text = parseAddress(selectedItem: selectedItem)
    print( cell.detailTextLabel?.text)
    return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  return 100
}

【讨论】:

  • 编译器向我显示了覆盖 numberOfRowsInSection 函数的建议。 @Sh_Khan
  • 当我搜索任何位置时应用程序崩溃。这是崩溃错误,由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“UITableView (; layer = ; contentOffset: {0, -56}; contentSize: {414, 44};adjustedContentInset: {56, 0, 226, 0}>) 未能从其数据源获取单元格
  • 在 swift 4.1 中不需要覆盖,也复制我在答案中添加的方法
  • 在线崩溃 let cell = tableView.dequeueReusableCell(withIdentifier: "cell")!这是它显示的错误, Fatal error: Unexpectedly found nil while unwrapping an Optional value 。 @Sh_Khan
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-01
  • 1970-01-01
  • 2021-11-20
  • 1970-01-01
  • 2011-03-04
  • 1970-01-01
相关资源
最近更新 更多