所以基本上我要做的是:
我在我的tableView中查看了从核心数据加载的Title和SubTitle。当我选择单元格时,我想要在该单元格中未显示但存储在同一实体中的属性将传递给我的ViewController中的3个不同的UITextField。
我准备好了prepareForSegue,但我错过了发送这些属性的内容和方式。
这是我的tableView
中的代码使用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;
}
}
}
这就是我在viewController中应该收到的内容。
var viaSegue1:String!
var viaSegue2:String!
var viaSegue3:String!
和
override func viewDidLoad()
{
super.viewDidLoad()
panelWidthTextField.text = viaSegue1
panelHightTextField.text = viaSegue2
panelPitchTextField.text = viaSegue3
}
希望有人可以帮忙解决这个问题。
答案 0 :(得分:2)
您应该使用footer {
display: table-cell;
vertical-align: middle;
text-align: center;
}
来管理表格的数据。
获取结果控制器可以避免因为从获取请求管理自己的结果数组而给您带来很多不便。
当需要将信息传递到目标视图控制器时,您可以直接从模型对象中检索其他属性。
NSFetchedResultsController
或者,您可以注入托管对象上下文和对象的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
}
}
}
}
,然后在目标视图控制器中获取该特定对象。
另外,你看起来好像在处理两个不同实体的数组。
objectID
您应该强力键入强制转换所有内容,因此Swift确切知道数组或集合中的托管对象的类型。如果您正在处理一组var manufactorer = [NSManagedObject]()
var model = [NSManagedObject]()
类型,则应该声明它。
另外,您可能希望使用比Ledinfo
和viaSegue1
更具描述性的名称。这使您的代码更易于阅读,理解和维护。