您好,我一直在尝试创建一个连接到 do list 的解析。令我困扰的部分是当我想从表中删除一个元素并且我希望在数据库中删除相同的对象时。问题是我可以删除模拟器中的任何对象,但它会从数据库中删除下一个对象,而不是触发的对象。
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if (editingStyle == UITableViewCellEditingStyle.Delete){
LM.myList.removeAtIndex(indexPath.row)
let myQuery = PFQuery(className: "list")
myQuery.findObjectsInBackgroundWithBlock({ (Objects , error ) -> Void in
if (error != nil){
print("Error was Found")
}else if let Object = Objects {
for MyObject in Object {
if (MyObject["title"] as! String == LM.myList[indexPath.row].title && MyObject["desc"] as! String == LM.myList[indexPath.row].Description)
{
//print(LM.myList[indexPath.row ].Description)
//print(LM.myList[indexPath.row ].title)
//print(MyObject["title"] as! String)
//print(MyObject["desc"] as! String)
MyObject.deleteInBackground()
MyObject.saveInBackground()
}
}
}
}
)
tableView.deleteRowsAtIndexPaths( [indexPath] , withRowAnimation: UITableViewRowAnimation.Automatic)
}
}
提前致谢:)
最佳答案
LM.myList.removeAtIndex(indexPath.row)
此行将从本地数据源中删除目标对象,因此当您稍后查询它以比较名称时,您会发现错误的对象。
您不需要进行查询和迭代。您应该从本地数据源获取该对象,删除它(从服务器中删除它),然后从本地数据源和表中删除它。
关于swift - 使用 swift 从表和解析数据源中删除数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35394967/