我tableviewcontroller
的代码如下所示
import UIKit
import CoreData
class NotesListTableViewController: UITableViewController {
var managedObjectContext : NSManagedObjectContext!
var entries: [NSManagedObject]!
override func viewDidLoad()
{
super.viewDidLoad()
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
managedObjectContext = appDelegate.managedObjectContext
// makes the searchbar stay in the current screen and not spill into the next screen
// definesPresentationContext = true
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.entries.count
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int
{
return 1
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("NotesCell", forIndexPath: indexPath)
// Configure the cell...
let entry1 = entries[indexPath.row]
cell.textLabel?.text = entry1.valueForKey("entry_body") as? String
let imageData2 = entry1.valueForKey("entry_image") as? NSData
if let imageData2 = imageData2
{
let myimage = UIImage(data:imageData2,scale:1.0)
cell.imageView?.image = myimage
}
return cell
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier! == "showNote"
{
let detailDisplay = segue.destinationViewController as! DetailDisplayViewController
let selectedIndexPath = tableView.indexPathForSelectedRow!
let entry = entries[selectedIndexPath.row]
detailDisplay.entry = entry
}
}
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
let entry1 = self.entries[indexPath.row]
self.managedObjectContext.deleteObject(entry1)
self.entries.removeAtIndex(indexPath.row)
self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
do
{
try managedObjectContext.save()
} catch {
print ("could not save the new entry ")
}
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.tabBarController?.tabBar.hidden = false
self.fetchEntries()
}
func fetchEntries()
{
let fetchRequest = NSFetchRequest(entityName:"Entry")
do {
let entryObjects = try managedObjectContext.executeFetchRequest(fetchRequest)
self.entries = entryObjects as! [NSManagedObject]
}catch let error as NSError
{
print ("could not save the new entry \(error.description) ")
}
tableView.reloadData()
}
答案 0 :(得分:0)
首先,在视图控制器中实现此协议:
class NotesListTableViewController: UITableViewController, UITableViewDataSource, UITableViewDelegate{
在你的类中声明一个UISearchController:
let searchController = UISearchController(searchResultsController: nil)
在方法viewDidLoad中:
searchController.searchBar.placeholder = "Search..."
searchController.searchBar.backgroundColor = UIColor.clearColor()
searchController.searchBar.searchBarStyle = UISearchBarStyle.Minimal
searchController.searchBar.tintColor = UIColor.blueColor()
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
definesPresentationContext = true
tableView.tableHeaderView = searchController.searchBar
使用以下方法扩展视图控制器:
extension YourViewController: UISearchBarDelegate {
// MARK: - UISearchBar Delegate
func searchBar(searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {
fetchEntries(searchBar.text!)
}
}
extension YourViewController: UISearchResultsUpdating {
// MARK: - UISearchResultsUpdating Delegate
func updateSearchResultsForSearchController(searchController: UISearchController) {
fetchEntries(searchController.searchBar.text!)
}
}
此时你需要创建一个过滤数组“条目”的函数,例如你可以这样做:
func fetchEntries(searchText: String) {
//To do your filter based in a String
tableView.reloadData()
}
答案 1 :(得分:0)
searchController.searchResultsUpdater = self
此行指示控制器将实现searchController的方法。你可以尝试下一个:
使用扩展的Intead,您可以尝试在控制器内部实现扩展方法。删除控制器的扩展,并将两个方法直接放在NotesListTableViewController中:
func searchBar(searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {
fetchEntries(searchBar.text!)
}
}
func updateSearchResultsForSearchController(searchController: UISearchController) {
fetchEntries(searchController.searchBar.text!)
}
答案 2 :(得分:0)
在控制器中放置两种扩展方法:
func searchBar(searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {
fetchEntries(searchBar.text!)
}
func updateSearchResultsForSearchController(searchController: UISearchController) {
fetchEntries(searchController.searchBar.text!)
}