Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ios, swift: multiple tables, single viewcontroller

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()
    }
}
like image 486
womblerone Avatar asked Oct 21 '22 00:10

womblerone


1 Answers

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.

like image 193
Danial Hussain Avatar answered Oct 22 '22 13:10

Danial Hussain