Ho visto domande simili, ma sono molto nuovo per iOS e non capisco abbastanza per applicare le risposte al mio scenario. Sto creando un'app per iPad con i dati di base e voglio una visualizzazione orizzontale che mostri due immagini di tabelle affiancate. Non so come specificare la seconda tabellaView dal mio file vc.swift. Questo codice visualizza due tabelle identiche. EDIT: ora posso specificare diverse visualizzazioni di tabelle, ma non posso inviare coredati diversi a ciascuna di esse. Il problema sembra iniziare con DidLoad, che non può vedere una tabellaView, quindi deve recuperare tutti i dati ogni volta.ios, swift: più tabelle, single viewcontroller
dati è dallo stesso Ente, ha solo diversi attributi (questo è il motivo per cui ho fatto una funzione di playerFetchRequest che prende un parametro - pensato che avrei potuto solo fare diverse richieste di riporto con parametri diff):
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()
}
}
Trovato! questo era in realtà due domande: 1. come avere due tabelle e 2. come avere due fetchResControllers. Questo post ha risposto al primo e questo ha risposto al secondo. http://stackoverflow.com/questions/26461219/ios-swift-core-data-multiple-tables – wellspokenman