如何在Tableviewcontroller中显示搜索结果?

时间:2015-10-10 19:09:57

标签: ios swift uitableview core-data core

我想在tableviewcontroller中显示搜索到的核心数据结果。我知道如何在tableview中显示核心数据,但我希望在tableviewcontroller中显示特定数据。

例如,当用户从uipickerview中选择特定城市时,则根据特定城市从tableviewcontroller中显示核心数据的结果。

下面列出了一些代码。

导入UIKit 导入CoreData

类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.
}
*/}

以及从城市查找数据的代码如下所示。

    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"
        }
    }
}

我想混淆这两个代码并相应地显示核心数据结果" city"选择。

2 个答案:

答案 0 :(得分:1)

您应该使用NSFetchedResultsController填充表格视图。在更新表视图之前,将动态谓词添加到其获取请求和performFetch

答案 1 :(得分:0)

经过我的项目指南(教授chirag pandya)的大量尝试和帮助后,

答案:

在第一个视图控制器中使用preparesegue方法, 给推塞赛的名字, 使用segue名称, 在第二个视图控制器中创建谓词并使用第一个viewcontroller。 对于搜索和排序非常有用

代码: 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