gpt4 book ai didi

ios - 单元格未出现在 UITableView 中 - Swift 3

转载 作者:搜寻专家 更新时间:2023-11-01 06:17:13 24 4
gpt4 key购买 nike

我有以下代码,旨在从 UISearchBar 获取用户输入,基于它创建并启动 MKLocalSearch,然后显示所有 MKMapItemUITableView 中的名称。以下代码不起作用,而是因错误而崩溃:

*** 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“尝试将第 0 行插入第 0 节,但更新后第 0 节中只有 0 行”

代码:

import UIKit
import MapKit

class TableViewController: UITableViewController, UISearchResultsUpdating {

var localSearchRequest:MKLocalSearchRequest!
var localSearch:MKLocalSearch!
var localSearchResponse:MKLocalSearchResponse!
var locations = [MKMapItem]()

@available(iOS 8.0, *)
public func updateSearchResults(for searchController: UISearchController) {
localSearch?.cancel()
localSearchRequest = MKLocalSearchRequest()
localSearchRequest.naturalLanguageQuery = searchController.searchBar.text
localSearch = MKLocalSearch(request: localSearchRequest)
localSearch.start { (localSearchResponse, error) -> Void in
if let response = localSearchResponse {
self.locations.removeAll()
for mapItem in response.mapItems {
self.locations.append(mapItem)
print(mapItem)
}
if !response.mapItems.isEmpty {
let indexPaths = (0..<response.mapItems.count).map { IndexPath(row: $0, section: 0) }
self.tableView.insertRows(at: indexPaths, with: .none)
}
}
}
}

override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}

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

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as UITableViewCell
let row = indexPath.row
cell.textLabel?.text = self.locations[row].name
return cell
}

let searchController = UISearchController(searchResultsController: nil)

override func viewDidLoad() {
super.viewDidLoad()
searchController.searchResultsUpdater = self
searchController.hidesNavigationBarDuringPresentation = false
searchController.dimsBackgroundDuringPresentation = false
searchController.searchBar.sizeToFit()
searchController.searchBar.tintColor = UIColor(colorLiteralRed: 1, green: 1, blue: 1, alpha: 1)
searchController.searchBar.placeholder = "Location"
searchController.searchBar.returnKeyType = UIReturnKeyType(rawValue: 6)!
self.tableView.tableHeaderView = searchController.searchBar
}

override func viewWillDisappear(_ animated: Bool) {
searchController.isActive = false
searchController.searchBar.isHidden = true
}

override func viewDidDisappear(_ animated: Bool) {
searchController.isActive = false
searchController.searchBar.isHidden = true
}
}

.
TableViewController socket :

TableViewController Outlets

最佳答案

如何在您的 viewDidLoad 中添加这一行?

tableView.register(UITableViewCell.classForCoder(), forCellReuseIdentifier: "Cell")

关于ios - 单元格未出现在 UITableView 中 - Swift 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41918724/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com