ios - 从 UITableView 中删除项目 - 从 Parse.com 获取数据

标签 ios objective-c uitableview parse-platform

我有一个 UITableview,它从 parse.com 加载数据并显示它。我希望它是可编辑的,以便当用户从 TableView 中删除一个项目时,它会从 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 {

我已经查看了其他问题并看到了可变对象,即前面的 retrieveFromParse 方法在 commitEditing 样式方法中不可用?

我尝试过这样的事情

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

但是说找不到对象

感谢您提前提供帮助

最佳答案

你试过的东西是指 self.objects,但我没看到你在其他任何地方使用 objects

从表中删除的方法是从数据源中删除,然后从 TableView 中删除。由于您也希望从解析中删除该对象,因此您还有一个额外的步骤。

// 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: 并在 block 中进行本地删除。

关于ios - 从 UITableView 中删除项目 - 从 Parse.com 获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25963502/

相关文章:

ios - 尝试更改帧大小的 UIView 动画在 iOS 8 中不起作用,但在 iOS 7 中不起作用

ios - 为什么 iOS 在运行单元测试时不调用我的 View Controller UITableViewDelegate 方法?

ios - UITableViewCell 的背景颜色问题

ios - 确定细胞的当前状态

ios - 在 Spritekit 中使用图像作为图 block 集

iphone - 如何检测用户是否在 UIScrollView 中向后滚动了一个 View ?

ios - SQLITE更新表问题

ios - UICollectionView 无法滚动查看整个最后一行

Objective-C - 使用 GDB 打印方法参数

ios - 使所有内容变暗,但选定的行 ios