谁能告诉我在这里做错了什么?我正在设计一个小应用程序,它是从我的核心数据中添加,编辑和删除用户。但是,当我选择一行时,我的应用程序崩溃,这应该带我到下一个viewController。 错误是:'致命错误:在解包可选值时意外发现nil' 在这个转换中准备segue函数失败了:让我们联系:Contact = fetchedResultController.objectAtIndexPath(indexPath!)as!接触
因此,当我点击一行时,它必须转到下一个视图控制器。
这是我的代码:
//类ContactsTableViewController
import UIKit
import CoreData
class ContactsTableViewController: UITableViewController, NSFetchedResultsControllerDelegate{
// MARK: - Properties
let context = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
var fetchedResultController: NSFetchedResultsController = NSFetchedResultsController()
// MARK: - Actions
@IBAction func toggleMe(sender: AnyObject) {
UberSideBar.toggleWindow()
}
override func viewDidLoad() {
super.viewDidLoad()
fetchedResultController = getFtechedResultController()
fetchedResultController.delegate = self
fetchedResultController.performFetch(nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
let numberOfSection = fetchedResultController.sections?.count
return numberOfSection!
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let numberOfinASection = fetchedResultController.sections?[section].numberOfObjects
return numberOfinASection!
}
// MARK: - Custom functions
func getFtechedResultController() -> NSFetchedResultsController {
fetchedResultController = NSFetchedResultsController(fetchRequest: taskFetchRequest(), managedObjectContext: context!, sectionNameKeyPath: nil, cacheName: nil)
return fetchedResultController
}
func taskFetchRequest() -> NSFetchRequest{
let fetchRequest = NSFetchRequest(entityName: "Contact")
let sortDescriptor = NSSortDescriptor(key: "name", ascending: true)
fetchRequest.sortDescriptors = [sortDescriptor]
return fetchRequest
}
func controllerDidChangeContent(controller: NSFetchedResultsController) {
// Refresh tableview to fetch all data
tableView.reloadData()
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("ContactCell", forIndexPath: indexPath) as! ContactcellTableViewCell
let contact = fetchedResultController.objectAtIndexPath(indexPath) as! Contact
println(contact.name)
cell.nameLabel?.text = contact.name
return cell
}
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
let managedObject: NSManagedObject = fetchedResultController.objectAtIndexPath(indexPath) as! NSManagedObject
context?.deleteObject(managedObject)
context?.save(nil)
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
performSegueWithIdentifier("editContact", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "editContact" {
let indexPath = tableView.indexPathForSelectedRow()
let contactController: NewCategoryViewController = segue.destinationViewController as! NewCategoryViewController
let contact:Contact = fetchedResultController.objectAtIndexPath(indexPath!) as! Contact
contactController.contact = contact
}
}
} 和
import UIKit
import CoreData
class NewCategoryViewController: UIViewController {
// MARK: - Properties
var contact: Contact? = nil
// initialize the core data context:
let context = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
// MARK: - Outlets
@IBOutlet weak var imageHolder: UIImageView!
@IBOutlet weak var nameField: UITextField!
@IBOutlet weak var emailField: UITextField!
@IBOutlet weak var phoneField: UITextField!
@IBOutlet weak var categoryField: UITextField!
// MARK: - Actions
@IBAction func savebtn(sender: AnyObject) {
let entity = NSEntityDescription.entityForName("Contact", inManagedObjectContext: context!)
let newContact = Contact(entity: entity!, insertIntoManagedObjectContext: context)
newContact.name = nameField.text
newContact.email = emailField.text
newContact.phone = phoneField.text
//newContact.photo = UIImageJPEGRepresentation(imageHolder.image, 1)
var error: NSError?
context?.save(&error)
if let errorSaving = error {
var alert = UIAlertController(title: "Alert", message: "Couldn't save contact !!!", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
} else {
nameField.text = ""
emailField.text = ""
phoneField.text = ""
var alert = UIAlertController(title: "Alert", message: "Contact added", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
}
override func viewDidLoad() {
super.viewDidLoad()
if contact != nil {
nameField.text = contact?.name
emailField.text = contact?.email
phoneField.text = contact?.phone
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
任何帮助都将非常感激
答案 0 :(得分:2)
您不能将prepareForSegue中的发件人强制转换为ContactcellTableViewCell,因为它的类型为ContactsTableViewController。
不是做你正在做的事情,为了获得indexPath,只需做
let indexPath = tableView.indexPathForSelectedRow()
并删除此行:
let cell = sender as! ContactcellTableViewCell