Ios, fast, basic data + several tables

I am very new to ios and fast. In one view, how can I send two different select queries to two different kinds of tables? I have a class level fetchReq function that uses NSPredicate to accept a parameter and gives me the various results I want. The only place that knows which table is a tablView function, but it looks like the decision about what data to load will be made right away in viewDidLoad. Can some soul help me restructure the main data code to get a different query for the selection for each table?

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
source share
2 answers

You need 2 fetchedResultsControllers with two different fetch requests for each table. If your table delegates and data sources are also this view controller, you need to switch and provide the appropriate content ... for example:

  func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { if (tableView == tableView2) { return fetchedResultController2.sections?[section].numberOfObjects } else { return fetchedResultController.sections?[section].numberOfObjects } } 

Another option would be to create 2 custom MYTableViewDataSource objects and set a data source for each kind of table for this ... This can make it more obvious when you discover unexpected behavior and simplify data management.

+1
source

Just set up two separate NSFetchedResultsController objects, one for each table:

 var forwardFetchedResultController: NSFetchedResultsController var defenderFetchedResultController: NSFetchedResultsController 

then in viewDidLoad create them with different NSFetchRequests for each. And in your tableView functions, use the correct selected result controller for the correct table.

+1
source

Source: https://habr.com/ru/post/1205049/


All Articles