2015-11-03 69 views
0

我想過濾一個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

回答

1

問題出在您的NSPredicate。寫它的方式你嘗試檢查games數組contain中的對象是否爲字符串。但是這個數組中的對象是Repository的實例,並且謂詞無法知道它對於包含某些東西的含義。爲了解決這個問題,你應該將謂詞改成這樣:

NSPredicate(format: "SELF.name CONTAINS[c] %@",searchString) 

這樣,謂詞會檢查是否Repository實例的name屬性包含所提供的搜索字符串。

我寫了一些關於NSPredicatehere的更長時間。

讓我知道如果有什麼不清楚。

+0

謝謝,讓我繞過那個錯誤,但然後它崩潰的下一行代碼與** NSForwarding:警告:對象0x7fac5bf7aa6​​0類'MakeItWork.Repository'沒有實現methodSignatureForSelector: - 前面的麻煩 無法識別的選擇器 - [ MakeItWork.Repository valueForKey:] ** 我讀到,如果你得到那個錯誤,你需要把:NSObject放在你的類旁邊,然後在init(){...}內放入super.init()。這給了我最後一行說**致命錯誤的另一個錯誤:陣列不能橋接Objective-C ** –

+0

我應該對當前的錯誤提出另一個問題,並將其標記爲答案,因爲您的答案解決了問題儘管出現了錯誤,我還是提到過? –

+0

嗯,它可能是最好的,因爲更多的人會注意到你的新問題。而且我還沒有使用'斯威夫特',不幸的是不知道如何進一步幫助你。 – Losiowaty