2015-08-20 229 views
0

之间赛格瑞这是我的代码:麻烦与ViewControllers

import UIKit 
import CoreData 

class ViewController: UIViewController { 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     var logo = UIImage(named: "no_pain_no_gain-_.jpg") 
     logoImage.image = logo 

     viewExercisesButton.frame = CGRectMake(-30,250,125,125) 
     viewExercisesButton.addTarget(self, action: "viewExercisesButtonTouch:", forControlEvents: UIControlEvents.TouchDown) 
     viewExercisesButton.setImage(imageViewExercises, forState: .Normal) 
     viewExercisesButton.imageEdgeInsets = UIEdgeInsetsMake(30,30,30,30) 
     self.view.addSubview(viewExercisesButton) 
    } 

    var moc = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext 
    var fetchedResultsController: NSFetchedResultsController? 
    var daysArray1 = [TrainingDay]() 

    @IBOutlet var logoImage: UIImageView! 

    var viewExercisesButton = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton 
    var imageViewExercises = UIImage(named: "plusbutton.png") 


    func viewExercisesButtonTouch(sender: UIButton!) { 
     performSegueWithIdentifier("goToDays", sender: self) 
     println("future event will be added, button working fine - view") 
    } 
} 

这第一个去:

import UIKit 
import CoreData 

class ViewExercisesViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, NSFetchedResultsControllerDelegate { 

    override func viewDidLoad() { 

     fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchTrainingDetails(), managedObjectContext: moc!, sectionNameKeyPath: nil, cacheName: nil) 
     fetchedResultsController?.delegate = self 
     fetchedResultsController?.performFetch(nil) 
     self.viewExerciseTableView.reloadData() 


     sundayButton.frame = CGRectMake(-30,50,125,125) 
     sundayButton.addTarget(self, action: "sundayButtonTouch:", forControlEvents: UIControlEvents.TouchDown) 
     sundayButton.setImage(imageSunday, forState: .Normal) 
     sundayButton.imageEdgeInsets = UIEdgeInsetsMake(30,30,30,30) 
     self.view.addSubview(sundayButton) 

     mondayButton.frame = CGRectMake(120,50,125,125) 
     mondayButton.addTarget(self, action: "mondayButtonTouch:", forControlEvents: UIControlEvents.TouchDown) 
     mondayButton.setImage(imageMonday, forState: .Normal) 
     mondayButton.imageEdgeInsets = UIEdgeInsetsMake(30,30,30,30) 
     self.view.addSubview(mondayButton) 

     tuesdayButton.frame = CGRectMake(270,50,125,125) 
     tuesdayButton.addTarget(self, action: "tuesdayButtonTouch:", forControlEvents: UIControlEvents.TouchDown) 
     tuesdayButton.setImage(imageTuesday, forState: .Normal) 
     tuesdayButton.imageEdgeInsets = UIEdgeInsetsMake(30,30,30,30) 
     self.view.addSubview(tuesdayButton) 

     wednesdayButton.frame = CGRectMake(-30,150,125,125) 
     wednesdayButton.addTarget(self, action: "wednesdayButtonTouch:", forControlEvents: UIControlEvents.TouchDown) 
     wednesdayButton.setImage(imageWednesday, forState: .Normal) 
     wednesdayButton.imageEdgeInsets = UIEdgeInsetsMake(30,30,30,30) 
     self.view.addSubview(wednesdayButton) 

     thursdayButton.frame = CGRectMake(70,150,125,125) 
     thursdayButton.addTarget(self, action: "thursdayButtonTouch:", forControlEvents: UIControlEvents.TouchDown) 
     thursdayButton.setImage(imageThursday, forState: .Normal) 
     thursdayButton.imageEdgeInsets = UIEdgeInsetsMake(30,30,30,30) 
     self.view.addSubview(thursdayButton) 

     fridayButton.frame = CGRectMake(170,150,125,125) 
     fridayButton.addTarget(self, action: "fridayButtonTouch:", forControlEvents: UIControlEvents.TouchDown) 
     fridayButton.setImage(imageFriday, forState: .Normal) 
     fridayButton.imageEdgeInsets = UIEdgeInsetsMake(30,30,30,30) 
     self.view.addSubview(fridayButton) 

     saturdayButton.frame = CGRectMake(270,150,125,125) 
     saturdayButton.addTarget(self, action: "saturdayButtonTouch:", forControlEvents: UIControlEvents.TouchDown) 
     saturdayButton.setImage(imageSaturday, forState: .Normal) 
     saturdayButton.imageEdgeInsets = UIEdgeInsetsMake(30,30,30,30) 
     self.view.addSubview(saturdayButton) 


    } 

    //VAR AND LET 

    var sundayButton = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton 
    var imageSunday = UIImage(named: "day.png") 

    var mondayButton = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton 
    var imageMonday = UIImage(named: "day.png") 

    var tuesdayButton = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton 
    var imageTuesday = UIImage(named: "day.png") 

    var wednesdayButton = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton 
    var imageWednesday = UIImage(named: "day.png") 

    var thursdayButton = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton 
    var imageThursday = UIImage(named: "day.png") 

    var fridayButton = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton 
    var imageFriday = UIImage(named: "day.png") 

    var saturdayButton = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton 
    var imageSaturday = UIImage(named: "day.png") 

    @IBOutlet var viewExerciseTableView: UITableView! 

    var daysArray = [TrainingDay]() 
    var detailsArray = [TrainingDetails]() 

    var moc = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext 
    var fetchedResultsController: NSFetchedResultsController? 

    // FUNCTIONS 

    func sundayButtonTouch(sender: UIButton!) { 
     println("future event will be added, button working fine - sunday") 
    } 

    func mondayButtonTouch(sender: UIButton!) { 
     println("future event will be added, button working fine - monday") 
    } 

    func tuesdayButtonTouch(sender: UIButton!) { 
     println("future event will be added, button working fine - tuesday") 
    } 

    func wednesdayButtonTouch(sender: UIButton!) { 
     println("future event will be added, button working fine - wednesday") 
    } 

    func thursdayButtonTouch(sender: UIButton!) { 
     println("future event will be added, button working fine - thursday") 
    } 

    func fridayButtonTouch(sender: UIButton!) { 
     println("future event will be added, button working fine - friday") 
    } 

    func saturdayButtonTouch(sender: UIButton!) { 
     println("future event will be added, button working fine - saturday") 
    } 

    // FETCH REQUEST METHODS 


    func fetchTrainingDay() -> NSFetchRequest { 
     let fetchRequest = NSFetchRequest(entityName: "TrainingDay") 
     fetchRequest.predicate = nil 

     let sortDescriptor = NSSortDescriptor(key: "day", ascending: true) 
     fetchRequest.sortDescriptors = [sortDescriptor] 
     fetchRequest.fetchBatchSize = 20 
     return fetchRequest 
    } 

    func fetchTrainingDetails() -> NSFetchRequest { 

     let currentDay = daysArray 
     let fetchRequest = NSFetchRequest(entityName: "TrainingDetails") 
     var predicate = NSPredicate(format: "trainingDay = %@", currentDay) 
     fetchRequest.predicate = predicate 
     let sortDescriptor1 = NSSortDescriptor(key: "exerciseName", ascending: true) 
     let sortDescriptor2 = NSSortDescriptor(key: "repsNumber", ascending: true) 
     let sortDescriptor3 = NSSortDescriptor(key: "setsNumber", ascending: true) 
     fetchRequest.sortDescriptors = [sortDescriptor1, sortDescriptor2, sortDescriptor3] 
     fetchRequest.fetchBatchSize = 20 
     return fetchRequest 
    } 

    //TABLE VIEW DELEGATE METHODS 
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return fetchedResultsController?.sections?[section].numberOfObjects ?? 0 
    } 

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

     let cellIdentifier = "exCell" 
     var cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as? UITableViewCell 
     if cell == nil { 
      cell = UITableViewCell(style: UITableViewCellStyle.Value2, reuseIdentifier: cellIdentifier) 
     } 
     let row = indexPath.row 
     println("\(row)") 
     let details = detailsArray[indexPath.row] 
     cell!.textLabel!.text = details.exerciseName 
     cell?.detailTextLabel?.text = "Sets: #\(details.setsNumber) Reps: #\(details.repsNumber)" 
     return cell! 

    } 

    // MARK: NSFetchedResultsControllerDelegate 
    func controllerWillChangeContent(controller: NSFetchedResultsController) { 
     self.viewExerciseTableView.beginUpdates() 
    } 
    func controller(controller: NSFetchedResultsController, 
     didChangeObject anObject: AnyObject, 
     atIndexPath indexPath: NSIndexPath?, 
     forChangeType type: NSFetchedResultsChangeType, 
     newIndexPath: NSIndexPath?) 
    { 
     switch(type) { 
     case .Insert: 
      if let newIndexPath = newIndexPath { 
       viewExerciseTableView.insertRowsAtIndexPaths([newIndexPath], 
        withRowAnimation:UITableViewRowAnimation.Fade) 
      } 
     case .Delete: 
      if let indexPath = indexPath { 
       viewExerciseTableView.deleteRowsAtIndexPaths([indexPath], 
        withRowAnimation: UITableViewRowAnimation.Fade) 
      } 
     case .Update: 
      break 
     case .Move: 
      if let indexPath = indexPath { 
       if let newIndexPath = newIndexPath { 
        viewExerciseTableView.deleteRowsAtIndexPaths([indexPath], 
         withRowAnimation: UITableViewRowAnimation.Fade) 
        viewExerciseTableView.insertRowsAtIndexPaths([newIndexPath], 
         withRowAnimation: UITableViewRowAnimation.Fade) 
       } 
      } 
     } 
    } 

    func controller(controller: NSFetchedResultsController, 
     didChangeSection sectionInfo: NSFetchedResultsSectionInfo, 
     atIndex sectionIndex: Int, 
     forChangeType type: NSFetchedResultsChangeType) 
    { 
     switch(type) { 
     case .Insert: 
      viewExerciseTableView.insertSections(NSIndexSet(index: sectionIndex), 
       withRowAnimation: UITableViewRowAnimation.Fade) 
     case .Delete: 
      viewExerciseTableView.deleteSections(NSIndexSet(index: sectionIndex), 
       withRowAnimation: UITableViewRowAnimation.Fade) 
     default: 
      break 
     } 
    } 
    func controllerDidChangeContent(controller: NSFetchedResultsController) { 
     viewExerciseTableView.endUpdates() 
    } 
} 

这儿,这第二个。

现在,当我尝试这两者之间Segue公司,出现这种情况:

未来事件将被添加,按钮做工精细 - 视图 2015年8月20日15:05:58.047飞度保险丝[941: 13395] - [Swift._SwiftDeferredNSArray longLongValue]:无法识别的选择器发送到实例0x7f920a651230 2015-08-20 15:05:58.053适合保险丝[941:13395] 终止应用程序由于未捕获的异常'NSInvalidArgumentException',原因:' - [Swift._SwiftDeferredNSArray longLongValue]:发送到实例的无法识别的选择器0x7f920a651230' 第一次掷出调用堆栈:

有人知道为什么会发生这种情况?

UPDATE

我不喜欢这样的:

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

     let cell = tableView.dequeueReusableCellWithIdentifier("exCell", forIndexPath: indexPath) as! UITableViewCell 

     let details = fetchedResultsController!.objectAtIndexPath(indexPath) as! TrainingDetails 
     cell.textLabel!.text = "\(details.exerciseName)" //CRASHES RIGHT HERE: Thread 1:EXC_BAD_ACCESS(code=1, address=0x0) 
     cell.detailTextLabel!.text = "Sets: #\(details.setsNumber) Reps: #\(details.repsNumber)" 

     return cell 

    } 
+0

我认为*问题是'fetchTrainingDetails'中的谓词,因为daysArray是空的。要检查,请尝试删除谓词并查看问题是否消失。 – pbasdf

+0

嗯,你说得对,但它在这里崩溃了:'let details = detailsArray [indexPath.row]'与错误:**致命错误:数组索引超出范围**。我打印了数组的索引,它说索引0.索引0如何超出范围? –

+0

哦,它在将核心数据中的3个属性(名称,代码和集合)保存后立即崩溃。 –

回答

0

要保存的东西正确的核心数据,这种方法应该是这样的:

func appendTrainingDetailsToArray() { 
     let nameLabel = exerciseName.text 
     namesArray.append(nameLabel) 
     let numberOfSets = setsNumber.text?.toInt() 
     setsArray.append(numberOfSets!) 
     let numberOfReps = repsNumber.text?.toInt() 
     repsArray.append(numberOfReps!) 

     let row = daysPickerView.selectedRowInComponent(0) 
     let currentDay = daysArray[row] 


     let detailsEntity = NSEntityDescription.entityForName("TrainingDetails", inManagedObjectContext: moc!) 
     let trainingdetails = TrainingDetails(entity: detailsEntity!, insertIntoManagedObjectContext: moc) 
     trainingdetails.exerciseName = exerciseName.text 
     trainingdetails.repsNumber = repsNumber.text! 
     trainingdetails.setsNumber = setsNumber.text! 
     trainingdetails.trainingDay = currentDay 

     var error: NSError? 
     moc?.save(&error) 

     if let err = error { 
      var status = err.localizedFailureReason 
      println("\(status)") 
     } else { 
      println("CURRENT SETTING: \(trainingdetails.trainingDay)") 
     } 
    } 

这样,代码工作正常!!学分到@pbasdf