从解析错误中删除信息

时间:2015-08-17 19:31:01

标签: ios swift parse-platform backend

我正在建立一个应用程序,允许用户查看应用程序中的内容,以及将信息添加到Parse数据库并删除信息。

在我的Parse核心中,我有一个名为“information”的类,其中的列名为“title”和“detail”。

我使用两个数组在表视图中显示数据:titleArray和detailArray。

这是我重新加载信息的方式,但在删除tableView中的最后一项时似乎失败了。

func reloadData () {




    var query = PFQuery(className:"information")
    query.findObjectsInBackgroundWithBlock {
        (objects: [AnyObject]?, error: NSError?) -> Void in

        if error == nil {

            self.titleArray.removeAll()
            self.detailArray.removeAll()


            if let objects = objects as? [PFObject] {

                for object in objects {


                    if let stringTitle = object.objectForKey("title") as? String {

                        self.titleArray.append(stringTitle)


                        if let stringDetail = object.objectForKey("detail") as? String {
                            self.detailArray.append(stringDetail)


                            self.tableView.reloadData()

                        }
                    }

                }


            } else {

                var errorAlert = UIAlertController(title: "Failed to Retrieve Data", message: ("Error: \(error!)"), preferredStyle: .Alert)

                errorAlert.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))

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





            }
        }


    }


}

如果我在应用程序中打开另一个视图控制器然后返回,一切正常,但如果我运行viewDidLoad方法它仍然有错误。

请帮帮我!我将非常感激。

2 个答案:

答案 0 :(得分:0)

我认为你必须在删除成功后重新加载tableView并从数据源中删除项目

objectToDelete.deleteInBackgroundWithBlock {
(success: Bool, error: NSError?) -> Void in
 if (success) {
  // Force a reload of the table — update your datasource first
  titleArray.removeAtIndex(row)
  detailArray.removeAtIndex(row)
  self.reloadData()
} else {
 // There was a problem, check error
}

答案 1 :(得分:0)

试试这个:

func deleteInformation (row:Int) {
var query = PFQuery(className:"information")
 query.whereKey("title", equalTo: titleArray[row])
query.findObjectsInBackgroundWithBlock {(objects: [AnyObject]?, error: NSError?) -> Void in
      if error == nil {         
        if let objects = objects as? [PFObject] {       
            for object in objects {                 
               object.deleteInBackground()
              }
            }
             self.tableView.reloadData()
          }
         }
       }


  func tableView(tableView: UITableView!, canEditRowAtIndexPath indexPath: NSIndexPath!) -> Bool {
return true
 }

   func tableView(tableView: UITableView!, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath!) {
if (editingStyle == UITableViewCellEditingStyle.Delete) {
    row = Int(indexPath.row)
    deleteInformation(row)


}

}

相关问题