通过UITableview Cell更新/编辑解析对象(目标C)

时间:2015-12-12 23:10:40

标签: objective-c uitableview parse-platform

我的tableview中充满了存储在parse中的当前用户数据。我希望能够单击解析数据的单元格,并能够在编辑屏幕中编辑该数据,然后能够保存它并在解析类中更新。我在解析开发人员网站上看到了有关更新对象https://parse.com/docs/ios/guide#objects-updating-objects

的代码
PFQuery *query = [PFQuery queryWithClassName:@"GameScore"];
[query getObjectInBackgroundWithId:@"xWMyZ4YEGZ"
                         block:^(PFObject *gameScore, NSError *error) {

gameScore[@"cheatMode"] = @YES;
gameScore[@"score"] = @1338;
[gameScore saveInBackground];
}];

但问题在于

[query getObjectInBackgroundWithId:@"xWMyZ4YEGZ"
                         block:^(PFObject *gameScore, NSError *error)

是指定的特定ObjectId的查询。如何更改此设置,以便当用户单击tableview单元格时,查询将由解析中的特定ObjectId运行,以便我可以编辑与ObjectId关联的字符串。任何教程或指导都非常感谢!

这是我的Tableview.m

@implementation ViewController

- (id)initWithCoder:(NSCoder *)aCoder
{
    self = [super initWithCoder:aCoder];
    if (self) {
        // Custom the table

        // The className to query on
        self.parseClassName = @"rep";

        // The key of the PFObject to display in the label of the default cell style
        self.textKey = @"name";

        // Whether the built-in pull-to-refresh is enabled
        self.pullToRefreshEnabled = YES;

        // Whether the built-in pagination is enabled
        self.paginationEnabled = NO;

        // The number of objects to show per page
        //self.objectsPerPage = 10;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(refreshTable:)
                                                 name:@"refreshTable"
                                               object:nil];
    {
        UIAlertView *Alert = [[UIAlertView alloc]  initWithTitle:@"Delete Cannot Be Undone" message:nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil,  nil];
        [Alert show];
    }

}
- (void)refreshTable:(NSNotification *) notification
{
    // Reload the recipes
    [self loadObjects];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"refreshTable" object:nil];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

- (PFQuery *)queryForTable

{
    PFQuery *query = [PFQuery queryWithClassName:@"rep"];
    [query whereKey:@"user" equalTo:[PFUser currentUser]];



    return query;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object
{
    static NSString *simpleTableIdentifier = @"RepCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }

    // Configure the cell

    UILabel *nameLabel = (UILabel*) [cell viewWithTag:101];
    nameLabel.text = [object objectForKey:@"name" ];

    UILabel *time = (UILabel*) [cell viewWithTag:102];
   time.text = [object objectForKey:@"time"];


    return cell;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{

    return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{

    // Remove the row from data model
    PFObject *objectToDel = [self.objects objectAtIndex:indexPath.row];

    [objectToDel deleteInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {



        UIAlertView *Alert = [[UIAlertView alloc]  initWithTitle:@"Item Was Deleted Successfully. Pull Down to Refresh Tab" message:nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil,  nil];
        [Alert show];
        [self loadView];

    }
     ];
}

- (void) objectsDidLoad:(NSError *)error
{
    [super objectsDidLoad:error];

    NSLog(@"error: %@", [error localizedDescription]);
}





@end

0 个答案:

没有答案