prepareForSegue索引0超出空数组

时间:2015-11-22 18:43:54

标签: ios swift parsing

我有一个TableViewController并且遇到了DetailViewController的问题。

在TableViewController中它可以工作但是当我点击一个Cell并调用时我收到错误“ - [__ NSArrayM objectAtIndex:]:索引0超出空数组的边界'”并且看起来数组是空的,但是我无法找出原因。 我将从下面的TableViewController类发布我的代码,希望它能帮助你找到我在这里做错了什么。

    import UIKit
    import Parse
    import ParseUI


    class MasterTableViewController: UITableViewController, PFLogInViewControllerDelegate, PFSignUpViewControllerDelegate {

        var noteObjects: NSMutableArray! = NSMutableArray()

        var currentObject: PFObject!

        @IBOutlet weak var homePageTableView: UITableView!

        var imageFiles = [PFFile]()
        var imageText = [String]()




        override func viewDidLoad() {
            super.viewDidLoad()

            var query = PFQuery(className: "Posts")
            query.orderByDescending("createdAt")
            query.findObjectsInBackgroundWithBlock{
                (posts: [AnyObject]?, error: NSError?) -> Void in

                if error == nil{

                    // success fetching objects

                    for post in posts!{

                    self.imageFiles.append(post["imageFile"] as! PFFile)
                    self.imageText.append(post["title"] as! String)

                    }

                    // reload the table
                    self.homePageTableView.reloadData()



                }else{

                    print(error)

                }


            }


        }


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


        /*** Table view begins ***/
        override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

            return imageText.count

        }


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

            let singleCell: MasterTableViewCell = tableView.dequeueReusableCellWithIdentifier("cell") as! MasterTableViewCell

            // title
            singleCell.titleImageLabel.text = imageText[indexPath.row]


            //images
            imageFiles[indexPath.row].getDataInBackgroundWithBlock{(imageData: NSData?, error: NSError?) -> Void in

                if imageData != nil{

                    let image = UIImage(data: imageData!)
                    singleCell.koiImageView.image = image

                }else{

                    print(error)

                }

            }

            return singleCell


        }



       override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

            self.performSegueWithIdentifier("editNote", sender: self)

        }

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


        if (segue.identifier == "editNote") {
            // get the new viewcontroller
            var koiDetail = segue.destinationViewController as! AddNoteTableViewController


            // pass selected object to the destination view controller
           if let indexPath = self.tableView.indexPathForSelectedRow {

            let row = Int(indexPath.row)

            koiDetail.object = (noteObjects?[row] as! PFObject)

            }
        }

        }



        override func viewDidAppear(animated: Bool) {
            super.viewDidAppear(animated)

            if (PFUser.currentUser() == nil) {

                let logInViewController = PFLogInViewController()

                logInViewController.delegate = self

                let signUpViewController = PFSignUpViewController()

                signUpViewController.delegate = self

                logInViewController.signUpController = signUpViewController

                self.presentViewController(logInViewController, animated: true, completion: nil)

            }else {

                self.fetchAllObjectsFromLocalDatastore()
                self.fetchAllObjects()

            }

        }

        func fetchAllObjectsFromLocalDatastore() {

            let query: PFQuery = PFQuery(className: "Werte")

            query.fromLocalDatastore()
            query.orderByDescending("createdAt")


            query.whereKey("username", equalTo: PFUser.currentUser().username)

            query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in

                if (error == nil) {

                    let temp: NSArray = objects as NSArray

                    self.noteObjects = temp.mutableCopy() as! NSMutableArray

                    self.tableView.reloadData()

                }else {

                    print(error.userInfo)

                }

            }

        }

        func fetchAllObjects() {

            PFObject.unpinAllObjectsInBackgroundWithBlock(nil)

            let query: PFQuery = PFQuery(className: "Note")

            query.whereKey("username", equalTo: PFUser.currentUser().username)

            query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in

                if (error == nil) {

                    PFObject.pinAllInBackground(objects, block: nil)

                    self.fetchAllObjectsFromLocalDatastore()

                }else {

                    print(error.userInfo)

                }

            }

        }


        // MARK: - Parse Login

        func logInViewController(logInController: PFLogInViewController!, shouldBeginLogInWithUsername username: String!, password: String!) -> Bool {

            if (!username.isEmpty || !password.isEmpty) {
                return true
            }else {
                return false
            }

        }

        func logInViewController(logInController: PFLogInViewController!, didLogInUser user: PFUser!) {
            self.dismissViewControllerAnimated(true, completion: nil)
        }

        func logInViewController(logInController: PFLogInViewController!, didFailToLogInWithError error: NSError!) {
            print("Failed to log in...")
        }

        func signUpViewController(signUpController: PFSignUpViewController!, shouldBeginSignUp info: [NSObject : AnyObject]!) -> Bool {

            if let password = info?["password"] as? String {
                return password.utf16.count >= 8
            }
            return false

        }

        func signUpViewController(signUpController: PFSignUpViewController!, didSignUpUser user: PFUser!) {
            self.dismissViewControllerAnimated(true, completion: nil)
        }

        func signUpViewController(signUpController: PFSignUpViewController!, didFailToSignUpWithError error: NSError!) {
            print("Failed to sign up...")
        }

        func signUpViewControllerDidCancelSignUp(signUpController: PFSignUpViewController!) {
            print("User dismissed sign up.")
        }
}

0 个答案:

没有答案