2014-10-20 60 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函數中,使用正確的提取結果控制器爲正確的表。