i would like to unpin an item that is in my tableview cell from parse. I save the object on another page, and query it in the main page, add that object to an array so it can be displayed in the table view. Now im not sure how to correctly select the corresponding parse object with whats in the array so that i can unpin it.
ideally i'd like to do this using the objectId just in case a user saves an object with the same name. how can i grab the objectId of whats displaying in my cell, and unpin it?
what query i use to add my objects to the array to then be displayed in my table view
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)
}
}
In the commitediting() method you don't need to query again because your array already has all the data from parse so you just need to delete straight from the array then reloadData().
You just need:
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()
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.