2014-09-21 26 views
0

我有一個UITableview從parse.com加載數據並顯示它。我想,這樣當用戶從parse.com會刪除它會刪除其表視圖的項目這是編輯從UITableView刪除項目 - 從Parse.com獲取數據

我用我自己的tableview和使用parseSimpleCell.h,.M自定義單元格

這裏是我的tableview的.h和.m文件

#import <UIKit/UIKit.h> 
#import <Parse/Parse.h> 
#import "ParseExampleCell.h" 

@interface FavoritesTableViewController : UITableViewController <UITableViewDelegate,UITableViewDataSource> { 
    NSArray *itemsArray; 
} 

@property (weak, nonatomic) IBOutlet UITableView *favItemsTable; 

@end 

這裏是.m文件

#import "FavoritesTableViewController.h" 

@interface FavoritesTableViewController() 

@end 

@implementation FavoritesTableViewController 



- (void)viewDidLoad { 
    [super viewDidLoad]; 

    [self performSelector:@selector(retrieveFromParse)]; 


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

- (void) retrieveFromParse { 

    PFUser *currentUser = [PFUser currentUser]; 

    PFQuery *query = [PFQuery queryWithClassName:@"UserFavourite"]; 
    [query whereKey:@"userIdString" equalTo:currentUser.objectId]; 
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) { 
     if (!error) { 
      itemsArray = [[NSArray alloc] initWithArray:objects]; 
     } 
     [_favItemsTable reloadData]; 
    }]; 
} 



#pragma mark - Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    // Return the number of sections. 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    // Return the number of rows in the section. 

    return itemsArray.count; 

} 

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

    ParseExampleCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    PFObject *tempObject = [itemsArray objectAtIndex:indexPath.row]; 

    cell.cellTitle.text = [tempObject objectForKey:@"item"]; 


    cell.tintColor = [UIColor redColor]; 

    return cell; 

} 


// Override to support conditional editing of the table view. 
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { 
    // Return NO if you do not want the specified item to be editable. 
    return YES; 
} 



// Override to support editing the table view. 
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 


} 


@end 

我需要用這種方法- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {什麼去幫助

我已經看過其他問題,但看到變量對象,這是得到前eh retrieveFromParse方法不可用在commitEditing樣式方法?

我已經試過這樣的事情

if (editingStyle == UITableViewCellEditingStyleDelete) { 
    PFObject *object = [self.objects objectAtIndex:indexPath.row]; 
    [object deleteInBackgroundWithBlock:^(BOOL succeeded, NSError *error) { 
     [self loadObjects]; 
    }]; 
} 

不過是說對象沒有找到

感謝您的幫助提前

回答

1

您嘗試的事情是指self.objects,但我不」 t看到任何你使用的地方objects

從表中刪除的方法是從數據源中刪除然後從表視圖中刪除。既然你想從解析中刪除對象,你還有一個額外的步驟。

// remove from datasource 
PFObject *object = itemsArray[indexPath.row]; 
[itemsArray removeObject:object]; 

// tell the table to update 
[tableView beginUpdates]; 
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; 
[tableView endUpdates 

// remove from parse 
[object deleteInBackground]; 

請注意,如果您立即查詢這些相同的對象,這可能會設置競爭條件。如果存在風險,則使用deleteInBackgroundWithBlock:並在塊中進行本地刪除。