2015-07-20 34 views
0

我有一個自定義表格視圖控制器,我試圖從分析表中提取數據。雖然我能夠將數據保存到這個解析數據,空白顯示了當使用相同的代碼來檢索:無法從分析表中檢索數據

代碼TVC:

import UIKit 

class SavedNoteTableViewController: UITableViewController { 

var noteObjects: NSMutableArray! = NSMutableArray() 


override func viewDidLoad() { 
    super.viewDidLoad() 


} 

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

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



    }else { 
     self.fetchAllObjectsFromLocalDatastore() 
     self.fetchAllObjects() 

    } 

} 

func fetchAllObjectsFromLocalDatastore() { 

    var query: PFQuery = PFQuery(className: "savedNotes") 

    query.fromLocalDatastore() 

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


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

     if (error == nil) { 

      var temp: NSArray = objects as NSArray! 


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

      self.tableView.reloadData() 

     }else { 

      println(error!.userInfo) 

     } 

    } 

} 


func fetchAllObjects() { 

    PFObject.unpinAllObjectsInBackgroundWithBlock(nil) 

    var query: PFQuery = PFQuery(className: "savedNotes") 

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

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

     if (error == nil) { 


      PFObject.pinAllInBackground(objects, block: nil) 

      self.fetchAllObjectsFromLocalDatastore() 


     }else { 

      println(error!.userInfo) 

     } 

    } 


} 




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 self.noteObjects.count 

} 

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = self.tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! SavedNoteTableViewCell 

    var object: PFObject = self.noteObjects.objectAtIndex(indexPath.row) as! PFObject 

    cell.tickerLabel?.text = object["ticker"] as? String 

    return cell 
} 


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


    /* 
    var upcoming: AddNoteTableViewController = segue.destinationViewController as! AddNoteTableViewController 

    if (segue.identifier == "editNote") { 

    let indexPath = self.tableView.indexPathForSelectedRow()! 

    var object: PFObject = self.noteObjects.objectAtIndex(indexPath.row) as! PFObject 

    upcoming.object = object; 

    self.tableView.deselectRowAtIndexPath(indexPath, animated: true) 

    } 
    */ 
} 



// 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) { 
    if editingStyle == .Delete { 

     var object: PFObject = self.noteObjects.objectAtIndex(indexPath.row) as! PFObject 



     // Delete the row from the data source 
     let objectToDelete = object 
     objectToDelete.deleteInBackgroundWithBlock { 
      (success: Bool, error: NSError?) -> Void in 
      if (success) { 
       // Force a reload of the table - fetching fresh data from Parse platform 
       // self.loadObjects() 
       // self.tableView.reloadData() 
       self.fetchAllObjectsFromLocalDatastore() 
       self.fetchAllObjects() 

      } else { 
       // There was a problem, check error.description 
      } 
     } 




    } 

} 

} 

代碼自定義的表格視圖單元格:

import UIKit 

class SavedNoteTableViewCell: UITableViewCell { 

@IBOutlet weak var tickerLabel: UILabel! 

override func awakeFromNib() { 
    super.awakeFromNib() 
    // Initialization code 
} 

override func setSelected(selected: Bool, animated: Bool) { 
    super.setSelected(selected, animated: animated) 

    // Configure the view for the selected state 
} 

} 

沒有出現錯誤消息,儘管存在數據庫中的記錄,但只顯示空白。任何建議感激!

+0

'savedNotes'類有一個叫做'username'場? – kRiZ

+0

是的,我做了第一件事,我仔細檢查:) – user2647092

回答

0

我認爲你是在正確的軌道上,但我與解析的經驗,當你到達那個代碼塊

query.findObjectsInBackgroundWithBlock { (objects:[AnyObject]?, error:NSError) -> Void in 
    { 
    if error == nil { 

     if let Newobjects = objects as! [PFObject]{ 

      // then you could do whatever you want with the new check objects 
      // put it in your array do 

     } 

    } 

    }