I saw similar questions, but I'm very new to iOS and don't understand enough to apply the answers to my scenario. I am making an iPad application with basic data and want two tables displayed in the landscape view. I do not know how to specify the second tableView from my vc.swift file. This code displays two identical tables. EDIT: Now I can specify different tables, but I can not send different cables to each of them. The problem seems to start with DidLoad, which cannot see the tableView, so every time you need to retrieve all the data.
The data belongs to the same object, it just has different attributes (thatβs why I created a function from playerFetchRequest that takes a parameter - I thought I could just make different fetch requests with diff parameters):
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 tableView: UITableView! //this is my second table - Ive connected it in the IB to this VC @IBOutlet var tableView2: UITableView! let managedObjectContext = (UIApplication.sharedApplication().delegate as AppDelegate).managedObjectContext var fetchedResultController: NSFetchedResultsController = NSFetchedResultsController() func playerFetchRequest(playerType: String) -> NSFetchRequest { let fetchRequest = NSFetchRequest(entityName: "Players") let sortDescriptor = NSSortDescriptor(key: "number", ascending: true) let filterForwards = NSPredicate(format: "%K = %@", "type", playerType) fetchRequest.sortDescriptors = [sortDescriptor] fetchRequest.predicate = filterForwards return fetchRequest } func getFetchedResultController(playerType: String) -> NSFetchedResultsController { fetchedResultController = NSFetchedResultsController(fetchRequest: playerFetchRequest("Forward"), 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 { var cell:CustomTableViewCell = self.tableView.dequeueReusableCellWithIdentifier("customCell") as CustomTableViewCell let player = fetchedResultController.objectAtIndexPath(indexPath) as DataModel cell.l2?.text = player.lastName + ", " + player.firstName cell.l1?.text = player.number 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) tableView.registerNib(nib, forCellReuseIdentifier: "customCell") fetchedResultController = getFetchedResultController("Forward") 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!) { tableView.reloadData() } }
source share