gpt4 book ai didi

ios - 单击搜索栏中的输入按钮后,如何更新 View Controller 以显示搜索结果?

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

现在我有了 UISearchBar(我没有添加 UISearchDisplayController)。现在我需要:当我在 UISearchBar 中键入一些词并按回车键时,CategoryListViewController 应该显示搜索结果。如果我没有在搜索栏中输入任何内容,CategoryListViewController 应该会显示所有数据。输入单词并单击回车按钮后,我可以获得输入的文本。但是如何显示搜索结果呢? (我使用核心数据来存储数据)。关于 CategoryListViewController:

import Foundation
import UIKit
import CoreData
class CategoryListViewController: UIViewController,UITableViewDataSource, UITableViewDelegate,UISearchBarDelegate
{


func numberOfSectionsInTableView(tableView: UITableView) -> Int {
guard let count = resultController?.sections?.count else
{
return 0
}

return count
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
if (searchCategory != nil)
{
return 1
}

guard let count = resultController?.sections?[section].numberOfObjects
else
{
return 0
}


return count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell:UITableViewCell

cell = tableView.dequeueReusableCellWithIdentifier("firstrow", forIndexPath: indexPath)

if let category = resultController?.objectAtIndexPath(indexPath) as? Category, let title = category.title,let images = category.image
{

cell.textLabel?.text = title


let number = images.count

cell.detailTextLabel?.text = "Contains \(number) images"

}

return cell
}
@IBOutlet weak var tableView: UITableView!


@IBOutlet weak var searchView: UISearchBar!

func searchBarCancelButtonClicked(searchBar: UISearchBar)
{
self.view.endEditing(true)
}



override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
switch segue.identifier {
case .Some("catSegue"):
let indexPath = tableView.indexPathForSelectedRow!
let viewController = segue.destinationViewController as! CatImagesViewController
viewController.navigationItem.title = "Cat Images"

tableView.deselectRowAtIndexPath(indexPath, animated: false)

if let category = resultController?.objectAtIndexPath(indexPath) as? Category, /*let title = category.title,*/ let images = category.image
{

viewController.target_category = category
viewController.number_of_cells = images.count

}


default:
super.prepareForSegue(segue, sender: sender)
}
}
// MARK: View Life Cycle
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
searchView.delegate = self

//--------------------------------
if (searchCategory != nil)
{
resultController = try? CatService.sharedCatService.catCategories(searchCategory)
}
else
{
resultController = try? CatService.sharedCatService.catCategories()

}


}

func searchBarSearchButtonClicked(searchBar: UISearchBar) {
// print("searchText2 \(searchBar.text)")

searchCategory = searchBar.text
self.viewDidLoad()
}

private var searchCategory:String?
private var resultController: NSFetchedResultsController?
private var imageController: NSFetchedResultsController?
}

关于获取搜索结果的核心数据部分:

    func catCategories(searchTerm: String? = nil) throws -> NSFetchedResultsController {
let fetchRequest = NSFetchRequest(entityName: "Category")

//let fetchRequest = NSFetchRequest(namedEntity: Image.self)

if let someSearchTerm = searchTerm
{
fetchRequest.predicate = NSPredicate(format: "title == %@", someSearchTerm )
print("somesearchTerm is \(someSearchTerm)")
}



fetchRequest.sortDescriptors = [NSSortDescriptor(key:"title",ascending: true)]

let mainQueueContext = CoreDataService.sharedCoreDataService.mainQueueContext
let fetchResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: mainQueueContext, sectionNameKeyPath: nil, cacheName: nil)

try fetchResultsController.performFetch()
return fetchResultsController

}

最佳答案

我也喜欢“就地”过滤,即不使用搜索 Controller 和覆盖搜索结果。我通常遵循这种模式。

如果搜索词是 nil 或没有内容,您应该将 fetched results controller 的 fetch 请求的谓词显式设置为 nil

fetchRequest.predicate = (someSearchTerm ?? "").characters.count == 0 ? nil :
NSPredicate(format:"title BEGINSWITH %@", someSearchTerm!)

其次,你需要实现这个UISearchBarDelegate方法

func searchBarSearchButtonClicked(searchBar: UISearchBar) {
searchBar.resignFirstResponder()
someSearchTerm = searchBar.text
fetchedResultsController.performFetch()
tableView.reloadData()
}

关于ios - 单击搜索栏中的输入按钮后,如何更新 View Controller 以显示搜索结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33511582/

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