這是我在網站上的第二個問題,所以對於問題格式有任何反饋意見。有人說這裏是我的問題:Swift 2:使用兩個CoreData實體填充一個TableView
我想填充這個tableview與兩個不同的CoreData實體,我不知道如何去做。起初,我嘗試創建另一個對象,併爲tableview做必要的方法,但它沒有奏效。我試圖用來自另一個實體的數據填充它,並且我無法讓tableview顯示來自第二個實體的信息。我想知道這樣做是否有任何已知的慣例。任何幫助深表感謝。
import UIKit
import CoreData
class DataEntryTableViewController: UITableViewController{
@IBOutlet weak var menuButton: UIBarButtonItem!
@IBOutlet var listaMiembros: UITableView!
var object = [NSManagedObject]()
var dictionary: [String:String] = [
"tipoVehiculo" :"",
"ocupantes" :"",
"numDeTablilla" :"",
"jurisdiccion": "",
"estado" :"",
"vin" :"",
"year" :"",
"marca": "",
"model" :"",
"marbete" :"",
"aseguradora" :"",
"fechaCompra": "",
"fechaExpiracion": "",
]
var objectNum = -1
@IBOutlet weak var listaMembers: UITableViewCell!
override func viewDidLoad() {
super.viewDidLoad()
listaMiembros.delegate = self
listaMiembros.dataSource = self
// Do any additional setup after loading the view, typically from a nib.
//ADD SCROLL VIEW DIMENTIONS
if revealViewController() != nil {
menuButton.target = revealViewController()
// menuButton.action = "revealToggle:"
menuButton.action = #selector(SWRevealViewController.revealToggle(_:))
tableView.registerClass(UITableViewCell.self,forCellReuseIdentifier: "Cell")
}
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
//1
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext
//2
let fetchRequest = NSFetchRequest(entityName: "PageFour")
//3
do {
let results = try managedContext.executeFetchRequest(fetchRequest)
object = results as! [NSManagedObject]
} catch let error as NSError {
print("Could not fetch \(error), \(error.userInfo)")
}
self.tableView.reloadData()
objectNum = -1
}
// MARK: UITableViewDataSource
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return object.count
}
override func tableView(tableView: UITableView,
cellForRowAtIndexPath
indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell")
let person = object[indexPath.row]
cell!.textLabel!.text = person.valueForKey("numDeTablilla") as? String
return cell!
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let indexPath = tableView.indexPathForSelectedRow // index path of selected cell
let cellIndex = indexPath!.row // index of selected cell
// print(indexPath?.row)//iT'S THE NUMBER YOU WANT - 1
let cellName = tableView.cellForRowAtIndexPath(indexPath!) // instance of selected cell
objectNum = cellIndex + 1
let appDel: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let context: NSManagedObjectContext = appDel.managedObjectContext
let request = NSFetchRequest(entityName: "PageFour")
request.returnsObjectsAsFaults = false
do {
try context.save()
} catch {
print("There was a problem!")
}
do {
let results = try context.executeFetchRequest(request)
if results.count > 0 {
for result in results as! [NSManagedObject] {
if cellName?.textLabel?.text == result.valueForKey("numDeTablilla") as? String{
dictionary["numDeTablilla"] = result.valueForKey("numDeTablilla") as? String
dictionary["marca"] = result.valueForKey("marcaField") as? String
dictionary["model"] = result.valueForKey("modeloField") as? String
dictionary["year"] = result.valueForKey("yearField") as? String
dictionary["tipoVehiculo"] = result.valueForKey("tipoVehiculo") as? String
dictionary["ocupantes"] = result.valueForKey("ocupantes") as? String
dictionary["jurisdiccion"] = result.valueForKey("jurisdicionVehiculo") as? String
dictionary["estado"] = result.valueForKey("estadoField") as? String
dictionary["vin"] = result.valueForKey("vinField") as? String
dictionary["marbete"] = result.valueForKey("numeroDeMarbete") as? String
dictionary["aseguradora"] = result.valueForKey("aseguradoraField") as? String
dictionary["fechaCompra"] = result.valueForKey("fechaCompraField") as? String
dictionary["fechaExpiracion"] = result.valueForKey("FechaExpiracionField") as? String
}
}
performSegueWithIdentifier("EditVehicle", sender: self)
}
} catch {
print("Fetch Failed")
}
}
@IBAction func unwindToVC(segue: UIStoryboardSegue) {
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "EditVehicle"){
//prepare for segue to the details view controller
if let detailsVC = segue.destinationViewController as? NewVehicleController {
// let indexPath = self.tableView.indexPathForSelectedRow
detailsVC.dictionary = self.dictionary
detailsVC.objectNum = self.objectNum
}
}
//unnecessary for now
// if(segue.identifier == "gotoNewData"){
// if let detailsVC = segue.destinationViewController as? NewDataEntryTableViewController {
// //detailsVC.dictionary = self.dictionary
// detailsVC.objectNum = self.objectNum
// }
// }
self.tableView.reloadData()
}
}
那麼確切的問題是什麼? – raki
你能添加一張圖片顯示你想要達到的目標嗎? – Priyal
我將它添加到了問題中,但我也會在此提及它。我想用來自第二個實體的信息填充tableview,但它不會工作,我不知道如何解決這個問題。 –