gpt4 book ai didi

ios - 在 UITableView 中实现 UISearchController 以过滤 User 对象数组

转载 作者:行者123 更新时间:2023-11-28 09:02:20 25 4
gpt4 key购买 nike

在 IOS 应用程序(swift 1.2、Xcode 6.3)上工作时,我正在尝试实现一个搜索栏,以便能够过滤和查找用户对象。但不幸的是,最后一个使用谓词过滤数组的方法 updateSearchResultsForSearchController 抛出了异常:

    2015-07-28 19:19:46.843 app.ios[34471:1158655] 
*** Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason:
'Can't use in/contains operator with collection app_ios.User (not a collection)

最后一个方法中 println(self.users) 的输出是:

[app_ios.User, app_ios.User, app_ios.User, app_ios.User, 
app_ios.User, app_ios.User, app_ios.User, app_ios.User,
app_ios.User, app_ios.User...]

管理 UserViewController 的代码是

import Foundation
import UIKit

class UsersViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

@IBOutlet var tableview:UITableView!

let apiClient = ApiClient()

var users: [User]!

var searchArray:[AnyObject] = [AnyObject](){
didSet {
self.tableview.reloadData()
}
}

var usersSearchController = UISearchController()

override func viewDidLoad() {
super.viewDidLoad()

self.usersSearchController = ({
// Two setups provided below:

// Setup One: This setup present the results in the current view.
let controller = UISearchController(searchResultsController: nil)
controller.searchResultsUpdater = self
controller.hidesNavigationBarDuringPresentation = false
controller.dimsBackgroundDuringPresentation = false
controller.searchBar.searchBarStyle = .Minimal
controller.searchBar.sizeToFit()
self.tableview.tableHeaderView = controller.searchBar

return controller
})()
}

override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
println("UsersController viewWillAppear")

apiClient.usersService.getList() { users, error in
if users != nil {
self.users = users
self.tableview?.reloadData()
} else {
println("error: \(error)")
}
}
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if ( self.usersSearchController.active){
return self.searchArray.count ?? 0

} else {
return self.users?.count ?? 0

}
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

var cell = tableView.dequeueReusableCellWithIdentifier("userObjectCell") as! UserTableViewCell

if (self.usersSearchController.active){
cell.userObject = self.searchArray[indexPath.row] as? User
return cell

} else {
cell.userObject = self.users?[indexPath.row]

return cell
}
}

}

extension UsersViewController: UITableViewDelegate
{
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
}

extension UsersViewController: UISearchResultsUpdating
{
func updateSearchResultsForSearchController(searchController: UISearchController)
{
self.searchArray.removeAll(keepCapacity: false)

let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchController.searchBar.text)
println(self.users)
let array = (self.users as NSArray).filteredArrayUsingPredicate(searchPredicate)
self.searchArray = array
}
}

最佳答案

感觉你没有设置搜索 Controller 的delegate和datasource给VC。

关于ios - 在 UITableView 中实现 UISearchController 以过滤 User 对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31684131/

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