使用parse swift删除表格视图单元格

时间:2016-07-06 23:15:12

标签: ios xcode swift

您好我一直在使用解析和快速创建应用,并且一直在尝试删除解析和屏幕上的单元格。我在这里尝试了大部分内容,但它不起作用。请帮忙。下面是代码:

import UIKit 

class TableViewController: PFQueryTableViewController {

    // Sign the user out
    @IBAction func signOut(sender: AnyObject) {

        PFUser.logOut()

        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let vc = storyboard.instantiateViewControllerWithIdentifier("SignUpInViewController") as! UIViewController
        self.presentViewController(vc, animated: true, completion: nil)
    }

    @IBAction func add(sender: AnyObject) {

        dispatch_async(dispatch_get_main_queue()) {
            self.performSegueWithIdentifier("TableViewToDetailView", sender: self)
        }
    }

    // Initialise the PFQueryTable tableview
    override init(style: UITableViewStyle, className: String!) {
        super.init(style: style, className: className)
    }

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)!

        // Configure the PFQueryTableView
        self.parseClassName = "Posts"
        self.textKey = "nameEnglish"
        self.pullToRefreshEnabled = true
        self.paginationEnabled = false
    }

    // Define the query that will provide the data for the table view
    override func queryForTable() -> PFQuery {
        var query = PFQuery(className: "Posts")
        query.orderByAscending("nameEnglish")
        return query
    }

    //override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell {

        var cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! PFTableViewCell!
        if cell == nil {
            cell = PFTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
        }

        // Extract values from the PFObject to display in the table cell
        if let nameEnglish = object?["nameEnglish"] as? String {
            cell.textLabel?.text = nameEnglish
        }
        if let capital = object?["capital"] as? String {
            cell.detailTextLabel?.text = capital
        }

        return cell
    }

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

        // Get the new view controller using [segue destinationViewController].
        var detailScene = segue.destinationViewController as! DetailViewController

        // Pass the selected object to the destination view controller.
        if let indexPath = self.tableView.indexPathForSelectedRow {
            let row = Int(indexPath.row)
            detailScene.currentObject = (objects?[row] as? PFObject)
        }
    }

    override func viewDidAppear(animated: Bool) {

        // Refresh the table to ensure any data changes are displayed
        tableView.reloadData()
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.reloadData()
        tableView.reloadInputViews()
    }

}

1 个答案:

答案 0 :(得分:1)

你的使命Ethan,你应该选择接受它......哈哈,我不得不说出来。

您想要从PFQueryTableViewController中删除单元格。所以你可以使用这样的东西:

<强>夫特

override func tableView(tableView: UITableView,
  commitEditingStyle editingStyle: UITableViewCellEditingStyle,
      forRowAtIndexPath indexPath: NSIndexPath) {
    if (editingStyle == .Delete) {
        removeObjectAtIndexPath(indexPath)
    }
}

<强>目标C

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

if (editingStyle == UITableViewCellEditingStyleDelete) {

    PFObject *object = self.objects[indexPath.row];
    object[@"hidden"] = @YES;
    [object saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error){

        if (!error) {

            [self removeObjectAtIndexPath:indexPath animated:YES];
            [self loadObjects];


        } else {

...其余的代码

但正如其他人所说,Parse正在关闭,因此您可能需要考虑Firebase或Parse Server。

祝你好运Ethan。