2014-10-20 63 views
0

我对ios和swift非常陌生。在单个视图中,如何向两个不同的表视图发送两个不同的提取请求?我有一个类级别的fetchReq函数,它使用NSPredicate来获取参数并给我所需的各种结果。唯一知道哪个表是哪个tablView func的地方,但它看起来像关于哪些数据加载立即在viewDidLoad上做出的决定。难道某种灵魂会帮助我重构核心数据代码,以便为每个表获取不同的提取请求?ios,swift,核心数据+多表

import UIKit 
import CoreData 

class CustomTableViewCell : UITableViewCell { 
    @IBOutlet var l1: UILabel? 
    @IBOutlet var l2: UILabel? 

    func loadItem(#number: String, name: String) { 
     l1!.text = number 
     l2!.text = name 
    } 
} 

class ViewController: UIViewController, UITableViewDelegate, NSFetchedResultsControllerDelegate, UITableViewDataSource { 

    @IBOutlet var tableView1: UITableView! 
    //this is my second table - Ive connected it in the IB to this VC. both tables work, but are identical 
    @IBOutlet var tableView2: UITableView! 
    let managedObjectContext = (UIApplication.sharedApplication().delegate as AppDelegate).managedObjectContext 
    var fetchedResultController: NSFetchedResultsController = NSFetchedResultsController() 


    //the filtering happens inside this function. it gets called via didLoad, not cellsForRows 
    func playerFetchRequest(playerType: String) -> NSFetchRequest { 
     let fetchRequest = NSFetchRequest(entityName: "Players") 
     let sortDescriptor = NSSortDescriptor(key: "number", ascending: true) 
     let filter = NSPredicate(format: "%K = %@", "type", playerType) 
     fetchRequest.sortDescriptors = [sortDescriptor] 
     fetchRequest.predicate = filter 
     return fetchRequest 
    } 

    func getFetchedResultController() -> NSFetchedResultsController { 
     fetchedResultController = NSFetchedResultsController(fetchRequest: playerFetchRequest(playerType), managedObjectContext:managedObjectContext!, sectionNameKeyPath: nil, cacheName: nil) 
     return fetchedResultController 
    } 

    //remember: to create a table with multiple sections just implement the numberOfSectionsInTableView(_:) method 
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     if let numberOfRowsInSection = fetchedResultController.sections?[section].numberOfObjects 
     {return numberOfRowsInSection} else {return 0} 
    } 

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     if (tableView == tableView2) { 
     var playerType = "Forward" 
     var cell:CustomTableViewCell = self.tableView1.dequeueReusableCellWithIdentifier("customCell") as CustomTableViewCell 
     let player = fetchedResultController.objectAtIndexPath(indexPath) as DataModel 
     cell.l2?.text = player.lastName + ", " + player.firstName 
     cell.l1?.text = player.number 
     println(tableView) 
     return cell 
     } 
     else { 
      var playerType = "Defender" 
      var cell:CustomTableViewCell = self.tableView2.dequeueReusableCellWithIdentifier("customCell") as CustomTableViewCell 
      let player = fetchedResultController.objectAtIndexPath(indexPath) as DataModel 
      cell.l2?.text = player.lastName + ", " + player.firstName 
      cell.l1?.text = player.number 
      println(tableView) 
      return cell 
     } 
    } 

    func tableView(tableView: UITableView!, didDeselectRowAtIndexPath indexPath: NSIndexPath!) { 
     tableView.deselectRowAtIndexPath(indexPath, animated: true) 
     println("You selected cell #\(indexPath.row)!") 
    } 

    override func viewDidLoad() { 
     var nib = UINib(nibName: "CustomTableViewCell", bundle: nil) 
     tableView1.registerNib(nib, forCellReuseIdentifier: "customCell") 
     tableView2.registerNib(nib, forCellReuseIdentifier: "customCell") 
     fetchedResultController = getFetchedResultController() 
     fetchedResultController.delegate = self 
     fetchedResultController.performFetch(nil) 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 
    func controllerDidChangeContent(controller: NSFetchedResultsController!) { 
     tableView1.reloadData() 
     tableView2.reloadData() 
    } 
} 
+0

这两个答案都是正确的 - 非常感谢您帮助新手出来! – wellspokenman 2014-10-20 08:58:25

回答

1

您需要2个fetchedResultsController,为每个表提供两个不同的提取请求。如果你的表委托和数据源都是这个视图控制器,你需要切换,并提供相应的内容......例如:

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

    if (tableView == tableView2) 
    { 
     return fetchedResultController2.sections?[section].numberOfObjects 
    } 
    else 
    { 
     return fetchedResultController.sections?[section].numberOfObjects 
    } 
} 

另一种选择是创建自定义2个对象MYTableViewDataSource并设置数据源对于每个表视图......当你有意想不到的行为并使数据更容易控制时,它可能会更明显。

1

就建立两个独立的NSFetchedResultsController对象,每个表:

var forwardFetchedResultController: NSFetchedResultsController 
var defenderFetchedResultController: NSFetchedResultsController 

然后viewDidLoad为每个不同的NSFetchRequests创建它们。并在您的tableView函数中,使用正确的提取结果控制器为正确的表。