作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
下面给出了我的 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()
}
最佳答案
首先,在你的 View Controller 中实现这个协议(protocol):
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
用这个扩展你的 View Controller :
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()
}
关于ios - 如何在从 coredata 加载的 tableviewcontroller 中搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38348988/
我是一名优秀的程序员,十分优秀!