使用 swift 从 table 和解析数据源中删除数据
deleting the data both from the table and the parse data source using swift
您好,我一直在尝试创建一个连接到待办事项列表的解析。困扰我的部分是当我想从 table 中删除一个元素并且我希望在数据库中删除相同的对象时。问题是我可以删除模拟器中的任何对象,但它会从数据库中删除下一个对象,而不是被触发的对象。
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)
此行从您的本地数据源中删除目标对象,因此当您稍后查询它以比较名称时,您会发现错误的对象。
您无需进行查询和迭代。您应该从本地数据源获取对象,将其删除(从服务器中删除),然后从本地数据源和 table.
中删除它
您好,我一直在尝试创建一个连接到待办事项列表的解析。困扰我的部分是当我想从 table 中删除一个元素并且我希望在数据库中删除相同的对象时。问题是我可以删除模拟器中的任何对象,但它会从数据库中删除下一个对象,而不是被触发的对象。
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)
此行从您的本地数据源中删除目标对象,因此当您稍后查询它以比较名称时,您会发现错误的对象。
您无需进行查询和迭代。您应该从本地数据源获取对象,将其删除(从服务器中删除),然后从本地数据源和 table.
中删除它