【问题标题】:parse swift - unpin an item in table view cell快速解析 - 在表格视图单元格中取消固定项目
【发布时间】: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


    【解决方案1】:

    commitediting() 方法中,您不需要再次查询,因为您的数组已经包含来自 parse 的所有数据,因此您只需直接从数组中删除然后 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()
     }
    

    【讨论】:

    • 当我只有该代码时,我滑动,单元格被删除,但是当表重新加载自身时,我刚刚尝试删除的单元格再次出现,因为解析对象仍在本地数据存储中。我想删除单元格并取消固定对象,这样当表格重新加载时它就永远消失了。
    • 你添加了reloadData()
    • 是的。但是当 viewdidload 方法运行时,这就是我的查询是在本地数据存储中获取对象并将它们添加到数组中,因此当它运行时,因为解析对象从未真正取消固定(只是单元格被删除)对象仍然被附加到数组中,并且仍然显示在表中。当单元格被删除时,我需要同时取消解析对象,但不知道如何从数组中定位与该单元格关联的对象。
    • 应用程序无法构建。在 object.deleteInBackground() 我得到“'localData'没有名为'deleteInBackground'的成员
    • 取出本地数据行