2015-09-08 27 views
0

我想从解析中取消固定在我的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) 
    } 
} 

回答

0

commitediting()方法,你不需要因为你的阵列已经再次查询有所有的数据解析,所以你只需要直接从数组中删除然后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() 
} 
+0

时,我只是有一个代码,我刷卡,细胞被删除,但是当表重新加载自身,该小区我只是试图删除,因为解析对象再次出现仍然在本地数据存储区中。我想删除单元格并取消固定对象,以便在表格重新加载完毕后保持不变。 – Bcavanaugh934

+0

您是否添加了reloadData() – Lamar

+0

yes。但是当viewdidload方法运行时,那就是我的查询要在本地数据存储中获取对象的位置,并将它们添加到数组中,因此当它运行时,因为解析对象从来没有真正解除锁定(只是单元被删除),对象仍然被附加到数组中,并且仍然显示在表中。当单元格被删除时,我需要同时解除对分析对象的锁定,但不知道如何将与该单元格关联的对象与数组关联。 – Bcavanaugh934

相关问题