简体   繁体   English

快速解析-取消固定表格视图单元格中的项目

[英]parse swift - unpin an item in table view cell

i would like to unpin an item that is in my tableview cell from parse. 我想从解析中取消固定我的tableview单元格中的项目。 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. 理想情况下,我希望使用objectId进行此操作,以防用户保存具有相同名称的对象。 how can i grab the objectId of whats displaying in my cell, and unpin it? 如何获取显示在单元格中的内容的objectId并取消固定?

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(). commitediting()方法中,您无需再次查询,因为您的数组已经具有所有要分析的数据,因此您只需要直接从数组中删除,然后再进行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()
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM