我想過濾一個JSON填充UITableView
,到目前爲止我已經嘗試了3或4個漂亮的教程,但是當我按下SearchBar
時,我得到的錯誤都是相同的。過濾Json填充的UITableView Swift 2
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Can't use in/contains operator with collection MakeItWork.Repository (not a collection'
唯一不同於我看到的教程是我的數組在另一個文件,我從那裏調用它。
Repository.swift
class Repository {
var name: String?
var releaseDate: String?
var gameImage: String?
var id: String?
init(json: NSDictionary) {
self.name = json["name"] as? String
self.releaseDate = json["release_date"] as? String
self.gameImage = json["image"] as? String
self.id = json["_id"] as? String
}
}
而且GameTableViewController.swift
var games = [Repository]()
var filteredTableData = [String]()
var shouldShowSearchResults = false
var searchController: UISearchController!
func configureSearchController() {
// Initialize and perform a minimum configuration to the search controller.
searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Search here..."
searchController.searchBar.delegate = self
searchController.searchBar.sizeToFit()
// Place the search bar view to the tableview headerview.
self.tableView.tableHeaderView = searchController.searchBar
}
func updateSearchResultsForSearchController(searchController: UISearchController) {
let searchString = searchController.searchBar.text!
filteredTableData.removeAll(keepCapacity: false)
let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@",searchString)
let array = (games as NSArray).filteredArrayUsingPredicate(searchPredicate)
filteredTableData = array as! [String]
self.tableView.reloadData()
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if shouldShowSearchResults {
return filteredTableData.count
}
else {
return games.count
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIdentifier = "GameTableViewCell"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! GameTableViewCell
if (shouldShowSearchResults) {
cell.nameLabel?.text = filteredTableData[indexPath.row]
return cell
}
else {
cell.nameLabel?.text = games[indexPath.row].name
cell.releaseDateLabel?.text = games[indexPath.row].releaseDate
if let url = NSURL(string: self.games[indexPath.row].gameImage!) {
cell.photoImageView?.hnk_setImageFromURL(url)
}
}
return cell
}
我把一個破發點,在updateSearchResultsForSearchController開始,我發現我每次按下的UISearchBar,在飛機墜毀前,因爲我還沒有輸入任何內容,所以它會搜索一個空searchString的循環。這是正常的嗎? 我查過的最後一個教程是http://www.ioscreator.com/tutorials/add-search-table-view-tutorial-ios8-swift。 我在這裏錯過了什麼?
編輯:我認爲這是正常的UISearchBar不斷更新,因爲我打電話UISearchResultsUpdating
。
謝謝,讓我繞過那個錯誤,但然後它崩潰的下一行代碼與** NSForwarding:警告:對象0x7fac5bf7aa60類'MakeItWork.Repository'沒有實現methodSignatureForSelector: - 前面的麻煩 無法識別的選擇器 - [ MakeItWork.Repository valueForKey:] ** 我讀到,如果你得到那個錯誤,你需要把:NSObject放在你的類旁邊,然後在init(){...}內放入super.init()。這給了我最後一行說**致命錯誤的另一個錯誤:陣列不能橋接Objective-C ** –
我應該對當前的錯誤提出另一個問題,並將其標記爲答案,因爲您的答案解決了問題儘管出現了錯誤,我還是提到過? –
嗯,它可能是最好的,因爲更多的人會注意到你的新問題。而且我還沒有使用'斯威夫特',不幸的是不知道如何進一步幫助你。 – Losiowaty