【发布时间】:2015-12-04 13:12:56
【问题描述】:
我想从解析中取消固定我的 tableview 单元格中的项目。我将对象保存在另一个页面上,并在主页中查询它,将该对象添加到数组中,以便它可以显示在表格视图中。现在我不确定如何使用数组中的内容正确选择相应的解析对象,以便我可以取消固定它。
理想情况下,我想使用 objectId 来执行此操作,以防用户保存具有相同名称的对象。如何获取单元格中显示的内容的 objectId 并取消固定它?
我使用什么查询将我的对象添加到数组中,然后显示在我的表格视图中
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)
}
}
【问题讨论】:
标签: ios swift uitableview parse-platform