Reputation: 29
I Want to display searched core data results in tableviewcontroller. I know how to display core data in tableview but I want specific data to be displayed in tableviewcontroller.
Like, when user selects a specific city from uipickerview, then results from core data are displayed in tableviewcontroller according to the specific city.
Some codes are listed below.
import UIKit
import CoreData
class MenuhospitalTableViewController: UITableViewController {
private var hospitalcoredata: [Hospitalcoredata] = []
var fetchResultController:NSFetchedResultsController!
override func viewDidLoad() {
super.viewDidLoad()
Load menu items from database
if let managedObjectContextt = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContextt {
let fetchRequest = NSFetchRequest(entityName: "Hospitalcoredata")
var e: NSError?
hospitalcoredata = managedObjectContextt.executeFetchRequest(fetchRequest, error: &e) as! [Hospitalcoredata]
if e != nil {
println("Failed to retrieve record: \(e!.localizedDescription)")
}
}
// Make the cell self size
self.tableView.estimatedRowHeight = 66.0
self.tableView.rowHeight = UITableViewAutomaticDimension
self.tableView.layoutIfNeeded()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// Return the number of sections.
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// Return the number of rows in the section.
return hospitalcoredata.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! MenuhospitalTableViewCell
// Configure the cell...
cell.nameLabel.text = hospitalcoredata[indexPath.row].namee
cell.contactnoLabel.text = hospitalcoredata[indexPath.row].contactnoo
// cell.priceLabel.text = "$\(menuItems[indexPath.row].price as! Double)"
return cell
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/}
and code to find data from city are given below..
let entityDescription =
NSEntityDescription.entityForName("Register",
inManagedObjectContext: managedObjectContext!)
let request = NSFetchRequest()
request.entity = entityDescription
let pred = NSPredicate(format: "(name = %@)", name.text)
request.predicate = pred
var error: NSError?
var objects = managedObjectContext?.executeFetchRequest(request,
error: &error)
if let results = objects {
if results.count > 0 {
let match = results[0] as! NSManagedObject
name.text = match.valueForKey("name") as! String
contactno.text = match.valueForKey("contactno") as! String
altno.text = match.valueForKey("altno") as! String
emailid.text = match.valueForKey("emailid") as! String
textf.text = match.valueForKey("bloodgroup") as! String
textff.text = match.valueForKey("city") as! String
status.text = "Matches found: \(results.count)"
} else {
status.text = "No Match"
}
}
}
I want to mix up these two codes and display core data results according to "city" selection.
Upvotes: 0
Views: 35
Reputation: 29
After lots of try and help of my project guide(prof. chirag pandya),
Answer:
use preparesegue method in first view controller, give name of push segue, use segue name, create predicate in second view controller and use first viewcontroller. vary useful for searching and sorting
Code: 1)IN (firstviewcontroller)
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if(segue.identifier=="segueTest"){
var svc = segue.destinationViewController as! MenuhospitalTableViewController
svc.toPass = textff.text
}
}
2)IN (secondviewcontroller)
let pred = NSPredicate(format: "(city = %@)", toPass)
fetchRequest.predicate = pred
Upvotes: 0
Reputation: 80265
You should be using a NSFetchedResultsController
to populate your table view. Add a dynamic predicate to its fetch request and performFetch
before you update your table view.
Upvotes: 1