简体   繁体   English

在解析Swift中删除tableview单元格

[英]Deleting tableview cell in parse Swift

I'm using Parse and I have a tableview with notes. 我正在使用Parse,并且具有带注释的tableview。 I am trying to add a delete function and with this code it slides and shows delete but when I press delete the app shuts down. 我正在尝试添加删除功能,并使用此代码滑动并显示删除,但是当我按Delete时,应用程序关闭。 Anyone know how to fix this? 有人知道怎么修这个东西吗?

import UIKit

class MasterTableViewController: UITableViewController, PFLogInViewControllerDelegate, PFSignUpViewControllerDelegate {
    var noteObjects: NSMutableArray! = NSMutableArray()

    override func viewDidLoad() {
        super.viewDidLoad()

        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem()
    }

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

        if (PFUser.currentUser() == nil) {
        }else {
            self.fetchAllObjectsFromLocalDatastore()
            self.fetchAllObjects()
        }
    }

    func fetchAllObjectsFromLocalDatastore() {
        var query: PFQuery = PFQuery(className: "Note")

        query.fromLocalDatastore()
        query.whereKey("username", equalTo: PFUser.currentUser()!.username!)
        query.orderByDescending("createdAt")
        query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in

            if (error == nil) {
                var temp: NSArray = objects!;  NSArray.self
                self.noteObjects = temp.mutableCopy() as! NSMutableArray
                self.tableView.reloadData()
            }else {
            }
        }
    }

    func fetchAllObjects() {
        PFObject.unpinAllObjectsInBackgroundWithBlock(nil)

        var query: PFQuery = PFQuery(className: "Note")
        query.whereKey("username", equalTo: PFUser.currentUser()!.username!)
        query.orderByDescending("createdAt")
        query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in

            if (error == nil) {
                PFObject.pinAllInBackground(objects, block: { (success, error) in
                    if error == nil {
                        self.fetchAllObjectsFromLocalDatastore()
                    }
                })
            }else {
                println(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 Potentially incomplete method implementation.
        // Return the number of sections.
        return 1
    }

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

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

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

        cell.masterTitleLabel?.text = object["title"] as? String
        cell.masterTextLabel?.text = object["text"] as? String

        return cell
    }

    override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {

        if editingStyle == UITableViewCellEditingStyle.Delete {
            if indexPath.row + 1 > self.noteObjects.count {
                return
            }
            if indexPath.row + 1 > self.noteObjects.count {
                return
            }

            // 1. Find the objects while they are still in the array
            var selectedPFlocation: AnyObject = self.noteObjects.objectAtIndex(indexPath.row)
            var selectedPTLocation: AnyObject = self.noteObjects.objectAtIndex(indexPath.row)

            // 2. Update the model by removing them from the array
            self.noteObjects.removeObjectAtIndex(indexPath.row)
            self.noteObjects.removeObjectAtIndex(indexPath.row)

            // 3. Delete the rows from the table view
            tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)

            // 4. Delete the actual data in your discretion
            selectedPFlocation.deleteInBackgroundWithBlock(nil)
            selectedPTLocation.deleteInBackgroundWithBlock(nil)

            // do not reload the table view
            // self.tableView.reloadData()
        }    
    }

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

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        var upcoming: AddNoteTableViewController = segue.destinationViewController as! AddNoteTableViewController

        if (segue.identifier == "editNote") {
            let indexPath = self.tableView.indexPathForSelectedRow()!

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

            upcoming.object = object

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

This is my working code example that deletes, parses, and also updates table data automatically. 这是我的工作代码示例,该示例自动删除,解析和更新表数据。

//delete records function
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
  if editingStyle == UITableViewCellEditingStyle.Delete {
    var cartClass = PFObject(className: CART_CLASS_NAME)
    cartClass = cartArray[indexPath.row] as! PFObject
    cartClass.deleteInBackgroundWithBlock {(success, error) -> Void in
      if error == nil {
        self.cartArray.removeObjectAtIndex(indexPath.row)
        tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
        print("successfully deleted!")
      } else {
        print("something wrong, please try again")
      }
    }
  }
}

If you force unwrap an Optional that contains nil, your app will crash. 如果您强制拆开包含nil的Optional,则您的应用程序将崩溃。 Try using ? 尝试使用? to unwrap instead of ! 解包而不是! . (Same for as? vs as! ) (同为as? vs as!

Maybe something like this? 也许是这样的吗?

func fetchAllObjectsFromLocalDatastore() 
{
    var query: PFQuery = PFQuery(className: "Note")
    query.fromLocalDatastore()
    query.whereKey("username", equalTo: PFUser.currentUser()!.username!)
    query.orderByDescending("createdAt")
    query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
        if (error == nil) 
        {
            self.noteObjects = objects?.mutableCopy() ?? NSMutableArray()
            self.tableView.reloadData()
        }
        else 
        {
        }
    }
}

Not you will invoke mutableCopy() on objects only if it's non-nil. mutableCopy() ,只有当它为非nil时, mutableCopy() objects调用mutableCopy() If it is nil, the result of objects?.mutableCopy() will be nil. 如果为nil,则objects?.mutableCopy()的结果为nil。 So we use the ?? 所以我们用?? (nil-coalescing) operator. (零销售)运算符。 ?? means "If the left side is nil, replace it with the right side" 表示“如果左侧为零,则用右侧替换”

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

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