【发布时间】:2025-11-23 21:20:03
【问题描述】:
这是我在网站上提出的第二个问题,因此我们非常感谢有关问题格式的任何反馈。话虽如此,这就是我的问题:
我正在尝试使用两个不同的 CoreData 实体填充此 tableview,但我不知道该怎么做。起初我尝试创建另一个对象并为 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()
}
}
【问题讨论】:
-
那么确切的问题是什么?
-
你能添加一张图片来显示你想要实现的目标吗?
-
我将其添加到问题中,但我也会在这里提及。我想用来自第二个实体的信息填充表格视图,但它不起作用,我不知道如何解决这个问题。
-
对第二个实体执行获取请求的代码部分在哪里?
标签: ios swift uitableview core-data swift2