gpt4 book ai didi

ios - 过滤一个 Json 填充的 UITableView Swift 2

转载 作者:行者123 更新时间:2023-11-29 01:30:38 24 4
gpt4 key购买 nike

我正在尝试过滤一个 JSON 填充的 UITableView,到目前为止,我已经尝试了 3 或 4 个非常好的教程,但是当我按下 时,我在所有这些教程上都遇到了相同的错误搜索栏

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Can't use in/contains operator with collection MakeItWork.Repository (not a collection'

唯一与我看到的教程不同的是我的数组在另一个文件上,我从那里调用它。

存储库.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 时,在崩溃之前,它都会使用 null searchString 进行搜索循环,因为我还没有在其中输入任何内容。那是正常的吗?我检查的最后一个教程是 http://www.ioscreator.com/tutorials/add-search-table-view-tutorial-ios8-swift .我在这里错过了什么?

编辑:我认为 UISearchBar 在我调用 UISearchResultsUpdating 时一直保持更新是正常的。

最佳答案

问题出在您的 NSPredicate 上。按照它的编写方式,您尝试检查 games 数组 中的对象是否包含 字符串。但是这个数组中的对象是 Repository 的实例,谓词无法知道 Repository 包含一些东西意味着什么。要解决此问题,您应该将谓词更改为:

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

这样,谓词将检查 Repository 实例的 name 属性是否包含提供的搜索字符串。

我写了一点关于 NSPredicate 的文章 here .

如果有什么不清楚的地方,请告诉我。

关于ios - 过滤一个 Json 填充的 UITableView Swift 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33500534/

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