5

Ive seen similar questions, but I'm very new to iOS and don't understand enough to apply the answers to my scenario. I'm making an iPad app with core data, and want a landscape view that displays two tableViews side by side. I don't know how to specify the second tableView from my vc.swift file. This code displays two identical tables. EDIT: I can specify different tableviews now, but I cannot send different coredata to each of them. The problem seems to start with DidLoad, which cannot see a tableView, so must fetch all data every time.

Data is from the same Entity, it just has different attributes (this is why I've made a function out of playerFetchRequest that takes a parameter - 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()
    }
}
1
  • Got it! this was actually two questions: 1. how to have two tables and 2. how to have two fetchResControllers. This post answered the first one and this one answered the second. stackoverflow.com/questions/26461219/… Commented Oct 20, 2014 at 8:59

1 Answer 1

3

I think you should check this way

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath    {
 if ([tableView isEqual: tableView1]) {
// Do something
  }

  else { // tableView == tableView2
// Do something else
  }
}

similar for other methods of tableview.

Sign up to request clarification or add additional context in comments.

10 Comments

part of whats messing me up is that tableView1 is actually called tableView. I'll rename it now and see if it still works, then attempt to implement the if statement.
You can use tableView because there is a difference between tableView the local variable and self.tableView the property
Ahh - thanks for explaining that tableView nomenclature. tableView is now tableView1 but I can't put the if statement inside didSelectRow, because that's not where it decides which data to retrieve. It does that up top, under playerFetchRequest.
i just need to tell you how to differenciate but the check should be like this. :)
tick my answer if you got the idea. So that in future any body can found answer easily thanks.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.