首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iOS MKLocalSearch引起的坠机

iOS MKLocalSearch引起的坠机
EN

Stack Overflow用户
提问于 2020-09-03 13:11:55
回答 1查看 77关注 0票数 0

我正在用一个MKLocalSearch和一个UITableView来执行UISearchBar。

第一次搜索总是很有效,但是如果你尝试另一次搜索,应用程序会崩溃,我会得到一个“超出范围的索引错误”。如果你需要更多的信息,请告诉我,谢谢。

代码语言:javascript
复制
    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
        self.searchResults = [] //this might be causing the crash
        print("searchText \(searchText)")
        let searchRequest = MKLocalSearch.Request()
        searchRequest.naturalLanguageQuery = searchText
        
        let search = MKLocalSearch(request: searchRequest)
        search.start { response, error in
            guard let response = response else {
                print("Error: \(error?.localizedDescription ?? "Unknown error").")
                return
            }

            for item in response.mapItems {
                self.searchResults.append(item.placemark)
                self.tableView.reloadData()
            }
        }
    }

}

//MARK: TableView
extension LocationSearchViewController: UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return searchResults.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "ResultCell", for: indexPath)
        let listItem = searchResults[indexPath.row]
        cell.textLabel?.text = listItem.name
        cell.detailTextLabel?.text = listItem.administrativeArea ?? ""

        return cell
    }
    

}

extension LocationSearchViewController: UITableViewDelegate {
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("selected a row")
        let local = searchResults[indexPath.row]
 
        // pass local back
        if let delegate = delegate{
            delegate.doSomethingWith(data: local)
        }
        
        
        dismiss(animated: true, completion: nil)
    }
}

在第三次或第四次尝试搜索之后,表中的结果保持不变,不再记录更多的结果。我知道错误:

错误:操作无法完成。(MKErrorDomain错误3.)

EN

回答 1

Stack Overflow用户

发布于 2020-09-03 13:19:12

你的问题是你过早地改变了searchResults。尝试:

代码语言:javascript
复制
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
    print("searchText \(searchText)")
    let searchRequest = MKLocalSearch.Request()
    searchRequest.naturalLanguageQuery = searchText
    
    let search = MKLocalSearch(request: searchRequest)
    search.start { response, error in
        guard let response = response else {
            print("Error: \(error?.localizedDescription ?? "Unknown error").")
            return
        }
        var newArray: [YourStruct] = []
        for item in response.mapItems {
            newArray.append(item.placemark)
            
        }
        self.searchResults = newArray
        self.tableView.reloadData()
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63724404

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档