快速解析-取消固定表格视图单元格中的项目 [英] parse swift - unpin an item in table view cell
问题描述
我想从解析中取消固定我的tableview单元格中的项.我将对象保存在另一页上,并在主页中对其进行查询,然后将该对象添加到数组中,以便可以在表视图中显示该对象.现在我不知道如何正确选择数组中的内容与之对应的解析对象,以便我可以将其固定.
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.
理想情况下,我希望使用objectId进行此操作,以防用户保存具有相同名称的对象.我该如何获取显示在单元格中的内容的objectId并取消固定呢?
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)
}
}
推荐答案
在 commitediting()方法中,您无需再次查询,因为您的数组已经具有解析出的所有数据,因此您只需直接从数组中删除,然后重新加载即可.
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().
您只需要:
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()
}
这篇关于快速解析-取消固定表格视图单元格中的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!