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

问题描述 投票:0回答:2

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

就像,当用户从 uipickerview 中选择特定城市时,核心数据的结果会根据特定城市显示在 tableviewcontroller 中。

下面列出了一些代码。

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

下面给出了从城市查找数据的代码..

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

我想混合这两个代码并根据“城市”选择显示核心数据结果。

ios swift uitableview core-data
2个回答
1
投票

您应该使用

NSFetchedResultsController
来填充表格视图。在更新表视图之前,向其获取请求添加动态谓词并
performFetch


0
投票

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

答案:

在第一个视图控制器中使用preparesegue方法, 给出推送序列的名称, 使用segue名称, 在第二个视图控制器中创建谓词并使用第一个视图控制器。 对于搜索和排序很有用

代码: 1)IN(第一个视图控制器)

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if(segue.identifier=="segueTest"){
        var svc = segue.destinationViewController as! MenuhospitalTableViewController
        svc.toPass = textff.text
    }
}

2)IN(第二个视图控制器)

let pred = NSPredicate(format: "(city = %@)", toPass)
        fetchRequest.predicate = pred
© www.soinside.com 2019 - 2024. All rights reserved.