UISearchBarController“工作”,但不在屏幕上显示

时间:2015-02-26 04:13:46

标签: ios swift core-data filter uisearchbar

我有一个用Swift编写的CoreData项目,我已经添加了UISearchController。我设置断点以查看搜索是否有效,我可以在控制台中打印filteredObjects的{​​{1}}列表,但它们不会显示在视图上。

基本上,当lldb未被使用时,我的应用中的单元格正常加载,但是当我在searchBar中输入内容时,我已经验证了{ {1}}已设置,对象正在添加到我的searchBar数组中,但我无法弄清楚他们为什么不在屏幕上显示。我认为我的问题"生活"这里:

searchPredicate

如果没有谓词,它的filteredObjects将返回它在override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell if searchPredicate == nil { self.configureCell(cell, atIndexPath: indexPath) } else { // configure the cell based on filteredObjects data if let note = self.filteredObjects?[indexPath.row] { cell.textLabel?.text = note.noteTitle } return cell } 中找到的内容。如果有cellForRowAtIndexPath,我需要从managedObjectContext的{​​{1}}数组中的searchPredicate对象返回信息。

我想我知道"什么"我需要做,但我不知道"怎么"部分。我无法弄清楚如何做到这一点。谢谢阅读。如果您有任何想法,我将不胜感激。

这是我的Note对象:

filteredObjects

另一个潜在的问题领域"可以是我的searchResultsController方法:

Note

我在这里设置了class Note: NSManagedObject { @NSManaged var dateCreated: NSDate @NSManaged var dateEdited: NSDate @NSManaged var noteTitle: String @NSManaged var noteBody: String }

configureCell

以下是我设置的相关属性和func configureCell(cell: UITableViewCell, atIndexPath indexPath: NSIndexPath) { let object = self.fetchedResultsController.objectAtIndexPath(indexPath) as! NSManagedObject cell.textLabel!.text = object.valueForKey("noteTitle")!.description }

numberOfRowsInSection

这是我的override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { if self.searchPredicate == nil { let sectionInfo = self.fetchedResultsController.sections![section] as! NSFetchedResultsSectionInfo return sectionInfo.numberOfObjects } else { return filteredObjects?.count ?? 0 } }

viewDidLoad

当搜索栏的搜索栏成为第一响应者时,会调用此委托方法。

    class MasterViewController: UITableViewController,
 NSFetchedResultsControllerDelegate, UISearchControllerDelegate,
 UISearchResultsUpdating, UISearchBarDelegate

// Properties
var detailViewController: DetailViewController? = nil
var managedObjectContext: NSManagedObjectContext? = nil

// Properties for UISearchController
var searchController: UISearchController!
var searchPredicate: NSPredicate?
var filteredObjects : [Note]? = nil

取消viewDidLoad:后,当我使用此委托方法设置override func viewDidLoad() { super.viewDidLoad() self.navigationItem.leftBarButtonItem = self.editButtonItem() if let split = self.splitViewController { let controllers = split.viewControllers let context = self.fetchedResultsController.managedObjectContext let entity = self.fetchedResultsController.fetchRequest.entity! self.detailViewController = controllers[controllers.count-1].topViewController as? DetailViewController } // UISearchController setup searchController = UISearchController(searchResultsController: nil) searchController.searchResultsUpdater = self searchController.dimsBackgroundDuringPresentation = false searchController.searchResultsUpdater = self searchController.delegate = self searchController.searchBar.sizeToFit() self.tableView.tableHeaderView = searchController?.searchBar self.tableView.delegate = self self.definesPresentationContext = true } &时,一切都会恢复正常// MARK: - UISearchResultsUpdating Delegate Method func updateSearchResultsForSearchController(searchController: UISearchController) { let searchText = self.searchController?.searchBar.text // steve put breakpoint println(searchController.searchBar.text) if let searchText = searchText { searchPredicate = NSPredicate(format: "noteBody contains[c] %@", searchText) filteredObjects = self.fetchedResultsController.fetchedObjects?.filter() { return self.searchPredicate!.evaluateWithObject($0) } as! [Note]? self.tableView.reloadData() println(searchPredicate) } } 回到零

searchBar

0 个答案:

没有答案