如何从解析服务器和本地存储中删除tableview单元

时间:2015-12-01 13:38:53

标签: ios swift parse-platform swift2

  import UIKit

class MasterTableViewController: UITableViewController, PFLogInViewControllerDelegate, PFSignUpViewControllerDelegate {


 // creating array for holding ojects 



var noteObjects: NSMutableArray! = NSMutableArray()
  var v = 0

override func viewDidLoad() {
    super.viewDidLoad()

}

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)



    if v == 0 {
        self.fetchAllObjectsFromLocalDataStore()
        //self.fetchAllObjects()


    }
}


// fetching data from local datastrore and from parse

func fetchAllObjectsFromLocalDataStore(){

    let query: PFQuery = PFQuery(className: "classname")
    query.orderByDescending("createdAt")

    query.fromLocalDatastore()

    query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in

        if (error == nil) {




            let temp: NSArray = objects as! NSArray

            self.noteObjects = temp.mutableCopy() as! NSMutableArray

             self.tableView.reloadData()

        }else {
        print(error!.userInfo)

        }
    }

}


func fetchAllObjects(){

   let query: PFQuery = PFQuery(className: "classname")
    query.orderByDescending("createdAt")

    query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in

        if (error == nil) {




            PFObject.pinAllInBackground(objects, block:  nil )


            self.fetchAllObjectsFromLocalDataStore()

           // self.tableView.reloadData()


        } else {
        print(error?.userInfo)

        }
 }
}





override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}



// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return self.noteObjects.count
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = self.tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! MasterTableViewCell

 let object : PFObject = self.noteObjects.objectAtIndex(indexPath.row) as! PFObject


    cell.MasterTitleLabel?.text = object["Title"] as? String
    cell.MasterTextLabel.text = object["Fstory"] as? String
    cell.MasterTimeLabel.text = object["Time"] as? String
    cell.MasterLocationLabel.text = object["Location"] as? String

    return cell
}

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    self.performSegueWithIdentifier("openStory", sender: self)

}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    let upcoming: AddNoteTableViewController = segue.destinationViewController as! AddNoteTableViewController

    if (segue.identifier == "openStory"){


        let indexPath = self.tableView.indexPathForSelectedRow!

        let object: PFObject = self.noteObjects.objectAtIndex(indexPath.row) as! PFObject

        upcoming.object = object

        self.tableView.deselectRowAtIndexPath(indexPath, animated: true)
}

}


@IBAction func btnReload(sender: AnyObject) {

    fetchAllObjects()


}


override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
    return true
}
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    if (editingStyle == UITableViewCellEditingStyle.Delete){


        noteObjects.removeObjectAtIndex(indexPath.row)
        self.tableView.reloadData()

  }
}
}

以上代码用于检索保存在Parse服务器中的对象能够检索数据并将其存储在我的本地数据存储中但我不知道如何从中删除所选单元格我的tableview什么时候从commitEditing.delete .....命令中删除它,它继续向我发送我在本地存储中的对象,所以如果有人知道如何删除这些东西而不是用代码解释我感谢有一个美好的一天: )

1 个答案:

答案 0 :(得分:0)

在从数组中删除对象之前,需要从数组中获取对象并在其上调用delete:

Lines <- "mark high_mark mark_cum 
      5        0        0        
      7        1        1       
      7        1        2       
      NA       0        2        
      7        1        3       
      7        1        4       
      NA       0        4"