2016-07-06 67 views
-1

嗨,我一直在使用解析和快速製作應用程序,並試圖刪除解析以及在屏幕上的單元格。我主要在這裏嘗試了一切,但它不起作用。請幫忙。繼承人的代碼:刪除表格視圖單元格與解析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() 
    } 

} 
+0

這裏沒有任何代碼試圖刪除任何東西 – Paulw11

+0

我知道我問我怎麼做,因爲一切嘗試不對應。任何想法如何做到這一點? @ Paulw11 –

+0

你想要刪除哪個單元格 - 它是如何識別的? – CoolPenguin

回答

1

你的任務伊桑,你應該選擇接受它....哈哈我不得不說。

您想從PFQueryTableViewController中移除單元格。所以,你可以使用像這樣:

斯威夫特

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

Objective-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 。

祝你好運伊桑。

+0

也是Ethan你應該編輯你的問題或者更改代碼或者一些東西。你會讓很多人把你的問題打倒。 – MattWright

+0

哈哈好吧,非常感謝你,只是有一個問題。當我嘗試你的代碼看起來不錯,但告訴我沒有這樣的事情,「removeObjectAtIndexPath」 –