Ios parse swift-在表视图单元格中取消项目绑定
我想从解析中取消绑定tableview单元格中的项目。我将对象保存在另一个页面上,并在主页中查询它,将该对象添加到一个数组中,以便它可以显示在表视图中。现在我不知道如何正确地选择数组中包含what的相应parse对象,以便取消绑定 理想情况下,我希望使用objectId执行此操作,以防用户使用相同的名称保存对象。我如何获取我手机中显示内容的objectId并取消锁定 我使用什么查询将对象添加到数组中,然后在表视图中显示Ios parse swift-在表视图单元格中取消项目绑定,ios,swift,uitableview,parse-platform,Ios,Swift,Uitableview,Parse Platform,我想从解析中取消绑定tableview单元格中的项目。我将对象保存在另一个页面上,并在主页中查询它,将该对象添加到一个数组中,以便它可以显示在表视图中。现在我不知道如何正确地选择数组中包含what的相应parse对象,以便取消绑定 理想情况下,我希望使用objectId执行此操作,以防用户使用相同的名称保存对象。我如何获取我手机中显示内容的objectId并取消锁定 我使用什么查询将对象添加到数组中,然后在表视图中显示 var selectedLighthouse: localData? = n
var selectedLighthouse: localData? = nil
var arrayToPopulateCells = [localData]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib
//query
let query = PFQuery(className: "ParseLighthouse")
query.fromLocalDatastore()
query.whereKey("User", equalTo: PFUser.currentUser()!)
query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
if error == nil {
// The find succeeded.
println("Successfully retrieved \(objects!.count) lighthouses.")
// Do something with the found objects
if let lighthouse = objects {
self.arrayToPopulateCells.removeAll(keepCapacity: true)
for object in lighthouse {
var singleData = localData()
singleData.name = object["Name"] as! String
singleData.note = object["Note"] as! String
singleData.date = object["Date"] as! String
singleData.latt = object["Latt"] as! NSNumber
singleData.longi = object["Longi"] as! NSNumber
singleData.lattDelta = object["LattDelta"] as! NSNumber
singleData.longiDelta = object["LongiDelta"] as! NSNumber
singleData.locality = object["Locality"] as! String
self.arrayToPopulateCells.append(singleData)
}
}
} else {
// Log details of the failure
println("Error: \(error!) \(error!.userInfo!)")
}
}
//setting table view datasource and delegate.
self.tableView.dataSource = self
self.tableView.delegate = self
var currentUser = PFUser.currentUser()
println(currentUser)
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// var lighthouse = self.lighthouses[indexPath.row]
var data = self.arrayToPopulateCells[indexPath.row]
//setting the prototype cell to link with the identifier set in attributes earlier.
let cell = tableView.dequeueReusableCellWithIdentifier("locationCell") as! lighthouseCell
let row = indexPath.row
cell.cellName.text = data.name
cell.cellPlace.text = data.locality
// cell.cellCoordinates.text = "\(lighthouse.latt)" + ", " + "\(lighthouse.longi)"
// cell.cellNote.text = lighthouse.note
cell.cellDate.text = "\(data.date)"
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.selectedLighthouse = self.arrayToPopulateCells[indexPath.row]
self.performSegueWithIdentifier("lighthouseDetailViewSegue", sender: self)
}
func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if (editingStyle == UITableViewCellEditingStyle.Delete) {
var arrayObjectId = localData()
var queryLocal = PFQuery(className:"ParseLighthouse")
queryLocal.fromLocalDatastore()
queryLocal.whereKey("Name", equalTo: arrayObjectId.name)
queryLocal.getObjectInBackgroundWithId(arrayObjectId.name) {
(parseLighthouse: PFObject?, error: NSError?) -> Void in
if error == nil && parseLighthouse != nil {
parseLighthouse?.unpinInBackground()
} else {
println(error)
}
}
self.arrayToPopulateCells.removeAtIndex(indexPath.row)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
}
}
在committediting()方法中,不需要再次查询,因为您的数组已经具有来自parse的所有数据,所以您只需要直接从数组中删除,然后重新加载data() 你只需要:
if (editingStyle == UITableViewCellEditingStyle.Delete)
{
var object:PFObject = self.arrayToPopulateCells[indexPath.row] as! PFObject
object.deleteInBackgroundWithBlock() // so check the if error == nil
self.arrayToPopulateCells.removeAtIndex(indexPath.row)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
self.tableView.reloadData()
}
当我只有那段代码时,我滑动,单元格被删除,但是当表重新加载时,我刚刚尝试删除的单元格再次出现,因为解析对象仍然在本地数据存储中。我想删除单元格并取消绑定对象,这样当表重新加载时,它就永远消失了。但是,当viewdidload方法运行时,我的查询就是在本地数据存储中获取对象,并将它们添加到数组中,因此当运行时,因为解析对象从未实际取消固定(只是删除了单元格),所以对象仍然会追加到数组中,并仍然显示在表中。当单元格被删除时,我需要同时取消绑定解析对象,但不知道如何从数组中定位与该单元格关联的对象。app不会生成。在object.deleteInBackground()上,我得到“'localData'没有名为'deleteInBackground'的成员”去掉该localData行