スウィフト:カントのUITableViewに行を挿入 - 私はアプリがエラーでクラッシュし、次のコード持っている3
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to insert row 0 into section 0, but there are only 0 rows in section 0 after the update'
をStackOverflowの上で記載されているように私には、複数の解決策を試してみましたが、どれもこのエラーを止めることはできません。
が試み:
コード:
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 localSearchResponse != nil {
let toNum = localSearchResponse?.mapItems.count ?? 0
for i in stride(from: 0, to: toNum, by: 1) {
self.locations.append((localSearchResponse?.mapItems[i])!)
print((localSearchResponse?.mapItems[i])!)
}
self.tableView.insertRows(at: [IndexPath(row: 0, section: 0)], with: UITableViewRowAnimation(rawValue: 5)!)
}
}
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.locations.count;
}
func tableView(tableView: UITableView, cellForRowAt indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath as IndexPath)
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 viewDidDisappear(_ animated: Bool) {
searchController.searchBar.isHidden = true
}
}
EDIT:をこれはまだ同じエラーが発生し
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)
}
}
}
、
attempt to insert row 0 into section 0, but there are only 0 rows in section 0 after the update
次のようヴァディアンが提案新しいlocalSearch.start
という使い方。
助けていただければ幸いです。
をお勧めしたいすべての行のアニメーションを使用しないように:戻りself.locations.countを。 ?そして、numberOfSections()メソッドを削除するだけで、1セクションが必要になります。 – Mamta