Код для моего контроллера просмотра приведен ниже.Как искать из таблицы, которая загружает данные из coredata
Табличное изображение загружает данные из основных данных.
Я добавил поисковую панель к viewcontroller.Can кто-нибудь поможет мне реализовать поиск в этом ?? Я смущен, глядя на учебники .please кто может обеспечить решение этой
import UIKit
import CoreData
class NotesListTableViewController: UITableViewController ,UISearchBarDelegate
{
@IBOutlet weak var searchBar: UISearchBar!
var managedObjectContext : NSManagedObjectContext!
var entries: [NSManagedObject]!
override func viewDidLoad()
{
super.viewDidLoad()
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
managedObjectContext = appDelegate.managedObjectContext
//self.fetchEntries()
// 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_title") 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.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()
}
}
где есть метод делегата поиска? Добавьте метод делегирования панели поиска и попробуйте выполнить поиск в coredata с помощью предикатов. –
, как я должен делать функ SearchBar (SearchBar: UISearchBar, textDidChange SearchText: String) { } метод симметричного, чтобы получить результат поиска ... Пожалуйста, помогите мне, я запутался о том, как сделать это –