[英]Passing core data from selected cell in tableview with certain attributes to another VC
So basically what I am trying to do is: 所以基本上我想做的是:
I have in my tableView a Title and SubTitle loaded from core data. 我在tableView中有一个从核心数据加载的Title和SubTitle。 When I select the cell, I want attributes that are not shown in that cell but stores in same entity to be passed to 3 different UITextField in my ViewController.
当我选择单元格时,我希望在该单元格中未显示但存储在同一实体中的属性传递给ViewController中的3个不同的UITextField。
I have my prepareForSegue set up and ready, but I am missing what and how to send those attributes. 我已经准备好我的prepareForSegue,但是我错过了什么以及如何发送这些属性。
This is the code from my tableView 这是我tableView中的代码
Updated Code with NSFetchedResultsController 使用NSFetchedResultsController更新了代码
class LedData: UIViewController, UITableViewDataSource, UITableViewDelegate, NSFetchedResultsControllerDelegate {
let ReuseIdentifierCell = "CellData"
// MARK: - IBOutlet
@IBOutlet var tableView: UITableView!
// MARK: - Variables
var managedObjectContext: NSManagedObjectContext!
lazy var fetchedResultsController: NSFetchedResultsController = {
// Initialize Fetch Request
let fetchRequest = NSFetchRequest(entityName: "Ledinfo")
// Add Sort Descriptors
let sortDescriptor = NSSortDescriptor(key: "manufactor", ascending: true)
fetchRequest.sortDescriptors = [sortDescriptor]
// Initialize Fetched Results Controller
let fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: self.managedObjectContext, sectionNameKeyPath: nil, cacheName: nil)
// Configure Fetched Results Controller
fetchedResultsController.delegate = self
return fetchedResultsController
}()
// MARK: - VC Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
do {
try self.fetchedResultsController.performFetch()
} catch {
let fetchError = error as NSError
print("\(fetchError), \(fetchError.userInfo)")
}
}
// MARK: -
// MARK: Table View Data Source Methods
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
if let sections = fetchedResultsController.sections {
return sections.count
}
return 0
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if let sections = fetchedResultsController.sections {
let sectionInfo = sections[section]
return sectionInfo.numberOfObjects
}
return 0
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(ReuseIdentifierCell, forIndexPath: indexPath) as! CellData
// Configure Table View Cell
configureCell(cell, atIndexPath: indexPath)
return cell
}
func configureCell(cell: CellData, atIndexPath indexPath: NSIndexPath) {
// Fetch Record
let record = fetchedResultsController.objectAtIndexPath(indexPath)
// Update Cell
if let manufactorer = record.valueForKey("manufactor") as? String {
cell.makerName.text = manufactorer
}
if let model = record.valueForKey("model") as? String {
cell.modelName.text = model
}
}
// MARK: -
// MARK: Table View Delegate Methods
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
// MARK: -
// MARK: Fetched Results Controller Delegate Methods
func controllerWillChangeContent(controller: NSFetchedResultsController) {
tableView.beginUpdates()
}
func controllerDidChangeContent(controller: NSFetchedResultsController) {
tableView.endUpdates()
}
func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
switch (type) {
case .Insert:
if let indexPath = newIndexPath {
tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
}
break;
case .Delete:
if let indexPath = indexPath {
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
}
break;
case .Update:
if let indexPath = indexPath {
let cell = tableView.cellForRowAtIndexPath(indexPath) as! CellData
configureCell(cell, atIndexPath: indexPath)
}
break;
case .Move:
if let indexPath = indexPath {
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
}
if let newIndexPath = newIndexPath {
tableView.insertRowsAtIndexPaths([newIndexPath], withRowAnimation: .Fade)
}
break;
}
}
}
This si what I have in my viewController that should receive. 这是我在viewController中应该收到的东西。
var viaSegue1:String!
var viaSegue2:String!
var viaSegue3:String!
and 和
override func viewDidLoad()
{
super.viewDidLoad()
panelWidthTextField.text = viaSegue1
panelHightTextField.text = viaSegue2
panelPitchTextField.text = viaSegue3
}
Hope someone can help with this. 希望有人可以帮助您。
What you should do is use an NSFetchedResultsController
to manage the data for your table. 您应该做的是使用
NSFetchedResultsController
管理表中的数据。
A fetched results controller will save you a lot of inconvenience from having to manage your own array(s) of results from a fetch request. 提取结果控制器将使您不必管理提取请求中自己的结果数组,从而为您节省了很多不便。
When it's time to pass the information to your destination view controller, you can retrieve the other attributes right from the model object. 当需要将信息传递给目标视图控制器时,您可以直接从模型对象中检索其他属性。
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showDetail" {
if let viewController = segue.destinationViewController as? ViewController {
if let indexPath = tableView.indexPathForSelectedRow {
let record = fetchedResultsController.objectAtIndexPath(indexPath) as! NSManagedObject // cast this as the type of your entity
viewController.panelWidth = record.panelWidth
viewController.panelHeight = record.panelHeight
viewController.panelPitch = record.panelPitch
}
}
}
}
Alternately, you could inject the managed object context and object's objectID
, then fetch that particular object in your destination view controller. 或者,您可以注入托管对象上下文和对象的
objectID
,然后在目标视图控制器中获取该特定对象。
Also, you make it appear like you're dealing with arrays of two different entities. 另外,您看起来就像处理两个不同实体的数组一样。
var manufactorer = [NSManagedObject]()
var model = [NSManagedObject]()
You should strongly type cast everything, so Swift knows exactly what type of managed object is in an array or set. 您应该强烈地强制类型转换所有内容,以便Swift确切知道数组或集合中托管对象的类型。 If you're dealing with an array of
Ledinfo
types, you should declare it as such. 如果要处理
Ledinfo
类型的数组,则应这样声明。
As an aside, you may want to use more descriptive names than viaSegue1
, and ViewController
. viaSegue1
,您可能想要使用比viaSegue1
和ViewController
更多的描述性名称。 This makes your code easier to read, understand, and maintain. 这使您的代码更易于阅读,理解和维护。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.