简体   繁体   English

删除tableView单元格SWIFT

[英]Deleting a tableView cell SWIFT

How can I delete a table view cell with Swift? 如何使用Swift删除表格视图单元格? I am using parse also. 我也在使用解析。 It would be great if someone can tell me how to make an action sheet asking, "Are you sure you want to delete?" 如果有人可以告诉我如何制作一个操作表,问“您确定要删除吗?”,那将是很好的。 If not that's okay. 如果没有,那没关系。

Here is the code I have so far: 这是我到目前为止的代码:

import UIKit

class MasterTableViewController: UITableViewController, UITableViewDelegate,
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.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.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, 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)

        }
    }
}

Implement commitEditingStyle method of tableview : 实现tableview的commitEditingStyle方法:

override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    if editingStyle == .Delete {
        let alert = UIAlertController(title: "Delete Note", message: "Are you sure you want to delete this note?", preferredStyle: .ActionSheet)

        alert.addAction(UIAlertAction(title: "Yes", style: .Destructive, handler: {
            (alert:UIAlertAction!) in
            //****//
             your code to delete item from parse.
            // ***//
             //And then remove object from tableview
             self.noteObjects.removeAtIndex(indexPath.row)
             self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
        }))
        alert.addAction(UIAlertAction(title: "No", style: .Cancel, handler: nil))

        self.presentViewController(alert, animated: true, completion: nil)
    }
}

Delete button will be visible on swiping left specific table cell Hope this will help! 在向左滑动特定表格单元格时将显示“ Delete按钮希望这会有所帮助!

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

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