gpt4 book ai didi

ios - UISearchController 显示不正确的结果

转载 作者:行者123 更新时间:2023-11-29 01:55:50 25 4
gpt4 key购买 nike

我的应用程序包含 UITableViewController、两个 UIViewController 和 Core Data。我想第一次制作 UISearchController。我查看了这个主题的其他问题,但我的 UISearchController 工作不正确。当我写入 searchBar 时,它给出了错误的结果。而且我不知道如何在表格 View 单元格中显示它们。请帮助我。

我在 UITableViewController 中的代码

var resultSearchController: UISearchController!
var searchPredicate: NSPredicate?
var filteredObjects: [Note]? = nil

override func viewDidLoad() {
super.viewDidLoad()

self.navigationItem.leftBarButtonItem = self.editButtonItem()

fetchedResultsController = NSFetchedResultsController(fetchRequest: allEmployeesFetchRequest(), managedObjectContext: managedObjectContext!, sectionNameKeyPath: "mynote", cacheName: "mynote") // both mynote
fetchedResultsController?.delegate = self
fetchedResultsController?.performFetch(nil)

// UISearchController setup
self.resultSearchController = ({
let controller = UISearchController(searchResultsController: nil)
controller.delegate = self
controller.searchResultsUpdater = self
controller.hidesNavigationBarDuringPresentation = false
controller.dimsBackgroundDuringPresentation = true
controller.searchBar.sizeToFit()
controller.searchBar.delegate = self
self.tableView.tableHeaderView = controller.searchBar
return controller
})()
self.tableView.reloadData()
}

func updateSearchResultsForSearchController(searchController: UISearchController) {
let searchText = self.resultSearchController.searchBar.text
println(searchText)
if let searchText = searchText {
searchPredicate = NSPredicate(format: "mynote contains[c] %@", searchText)
filteredObjects = self.fetchedResultsController?.fetchedObjects?.filter() {
return self.searchPredicate!.evaluateWithObject($0)
} as! [Note]?
self.tableView.reloadData()
println(searchPredicate)
}
}

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
if searchPredicate == nil {
return fetchedResultsController?.sections?.count ?? 0
} else {
return 1
}
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if searchPredicate == nil {
return fetchedResultsController?.sections?[section].numberOfObjects ?? 0 //0 default
} else {
return filteredObjects?.count ?? 0
}
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("CellNote", forIndexPath: indexPath) as! UITableViewCell
if searchPredicate == nil {
if var cellContact = fetchedResultsController?.objectAtIndexPath(indexPath) as? Note {
cell.textLabel?.text = cellContact.mynote
cell.detailTextLabel?.text = cellContact.mytime
return cell
}
}
return cell
}

我的核心数据类有

import Foundation
import CoreData
@objc(Note)
class Note: NSManagedObject {

@NSManaged var mynote: String
@NSManaged var mytime: String

}

最佳答案

我有一个用于多个查询的类,我只传递一个谓词,有时是一个排序,然后返回实体类对象的数组。

class QueryDatabase: NSObject {
private var _managedObjectContext: NSManagedObjectContext?

init(managedObjectContext: NSManagedObjectContext){
super.init()
_managedObjectContext = managedObjectContext
}

func QueryTable(entityName: String, sortBy: String?, ascending: Bool, searchPredicate: NSPredicate?) -> AnyObject?{
if _managedObjectContext != nil {
let fetchRequest = NSFetchRequest(entityName: entityName)
if(sortBy != nil){
let sort = NSSortDescriptor(key: sortBy!, ascending: ascending)
fetchRequest.sortDescriptors = [sort]
}

if(searchPredicate != nil){
fetchRequest.predicate = searchPredicate!
}

var err: NSError?
if let fetchResults = _managedObjectContext!.executeFetchRequest(fetchRequest, error: &err){
return fetchResults
}else{
NSLog("ERROR:\(err?.description)")
}
}
return nil
}
}

我将它与 searchBar 结合使用,但在我的例子中,我的谓词在其定义上添加了另一个修饰符 [cd]:

func findPricesBySearchCriteria(searchCriteria: String) -> [Prices]{
if(_managedObjectContext != nil){
let queryDatabase = QueryDatabase(managedObjectContext: _managedObjectContext!)
if let results = queryDatabase.QueryTable("Prices", sortBy: "item_description", ascending: true, searchPredicate: NSPredicate(format: "item_description contains[cd] %@", searchCriteria)) as? [Prices]{
arrSearch = results
}
}
return arrSearch
}

关于ios - UISearchController 显示不正确的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30866269/

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