按下TableViewCell后Swift崩溃

时间:2015-07-24 16:11:45

标签: ios swift tableviewcell

当我选择TableViewCell时,我的应用程序不断崩溃,但它没有给我一个错误消息。希望有些人可以提供帮助。下面是TableView控制器和View Controller代码。我已将日期添加到有线电视模型中,并认为它与此有关。

import UIKit
import CoreData



class DiveLogTableViewController: UITableViewController {

var myDivelog : Array<AnyObject> = []

override func viewDidLoad() {
    super.viewDidLoad()

    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = false

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem()
}

override func viewDidAppear(animated: Bool) {
    let appDel:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
    let context:NSManagedObjectContext = appDel.managedObjectContext!
    let freq = NSFetchRequest(entityName: "Divelog")

    myDivelog = context.executeFetchRequest(freq, error: nil)!
    tableView.reloadData()
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if segue.identifier == "update" {

    var selectedItem: NSManagedObject = myDivelog[self.tableView.indexPathForSelectedRow()!.row] as! NSManagedObject

    let ADLVC: AddDiveLogViewController = segue.destinationViewController as! AddDiveLogViewController

    ADLVC.divenumber = selectedItem.valueForKey("divenumber") as! String
    ADLVC.ddate = selectedItem.valueForKey("ddate") as! NSDate
    ADLVC.divelocation = selectedItem.valueForKey("divelocation") as! String
    ADLVC.existingItem = selectedItem

    }
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete method implementation.
    // Return the number of rows in the section.
    return myDivelog.count
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let CellID: NSString = "Cell"

    var cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier(CellID as String) as! UITableViewCell

    if let ip = indexPath as NSIndexPath? {
        var data: NSManagedObject = myDivelog[ip.row] as! NSManagedObject
        var ddate = data.valueForKey("ddate") as! NSDate
        var diveloc = data.valueForKey("divelocation") as! String
        var diveno = data.valueForKey("divenumber") as! String


        cell.textLabel!.text = "#\(diveno)@\(diveloc)"
        cell.detailTextLabel!.text = "\(ddate),location: \(diveloc)"
    }

    // Configure the cell...

    return cell
}



// Override to support conditional editing of the table view.
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
    // Return NO if you do not want the specified item to be editable.
    return true
}



// Override to support editing the table view.
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {

    let appDel:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
    let context:NSManagedObjectContext = appDel.managedObjectContext!

    if editingStyle == UITableViewCellEditingStyle.Delete {
        if let tv = tableView as UITableView? {
            context.deleteObject(myDivelog[indexPath.row] as! NSManagedObject)
            myDivelog.removeAtIndex(indexPath.row)
            tv.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Fade)
        }
        var error: NSError? = nil
        if !context.save(&error) {
            abort()
        }
    }
}


}

import UIKit
import CoreData

class AddDiveLogViewController: UIViewController {


@IBOutlet weak var textFieldDiveNumber: UITextField!

@IBOutlet weak var textFieldDiveLocation: UITextField!
@IBOutlet weak var textFieldDDate: UITextField!

var divenumber: String = ""
var divelocation: String = ""

var ddate = NSDate()

var datePickerView: UIDatePicker!

var existingItem: NSManagedObject!

override func viewDidLoad() {
    super.viewDidLoad()

    if (existingItem != nil) {
        textFieldDiveNumber.text = divenumber

        textFieldDiveLocation.text = divelocation
        textFieldDDate.text = ddate.stringValue
                }

    // Do any additional setup after loading the view.

    datePickerView = UIDatePicker()
    datePickerView.datePickerMode = UIDatePickerMode.Date

    var toolbar = UIToolbar(frame: CGRectMake(0, 0, datePickerView.frame.width, 44))
    let OKButton = UIBarButtonItem(title: "OK", style: .Plain, target: self, action: "OKButtonTapped:")

    toolbar.setItems([OKButton], animated: true)

    self.textFieldDDate.inputView = datePickerView
    self.textFieldDDate.inputAccessoryView = toolbar

}

@IBAction func saveTapped(sender: AnyObject) {

    let appDel: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate

    let contxt: NSManagedObjectContext = appDel.managedObjectContext!
    let en = NSEntityDescription.entityForName("Divelog", inManagedObjectContext: contxt)

    if (existingItem != nil) {
        existingItem.setValue(textFieldDiveNumber.text, forKey: "divenumber")

        existingItem.setValue(textFieldDiveLocation.text, forKey: "divelocation")
        existingItem.setValue(textFieldDDate.text.dateValue!, forKey: "ddate")

    } else {
        var newItem = Divelog(entity: en!, insertIntoManagedObjectContext: contxt)

        newItem.divenumber = textFieldDiveNumber.text

        newItem.divelocation = textFieldDiveLocation.text
        newItem.ddate = textFieldDDate.text.dateValue!

    }



    contxt.save(nil)

    self.navigationController?.popToRootViewControllerAnimated(true)
}

@IBAction func cancelTapped(sender: AnyObject) {
    self.navigationController?.popToRootViewControllerAnimated(true)
}

func OKButtonTapped(sender: UIBarButtonItem) {
    self.textFieldDDate.endEditing(true)
    self.textFieldDDate.text = datePickerView.date.stringValue
}


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
}

0 个答案:

没有答案