简体   繁体   中英

Delete row from table view iOS Parse.com

I am trying to delete a selected row from my tableView with below code

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];

    PFUser *user = [self.friends objectAtIndex:indexPath.row];

    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"Remove user"]
                                                        message:[NSString stringWithFormat:@"Are you sure you want to remove user?"]
                                                       delegate:self
                                              cancelButtonTitle:@"Cancel"
                                              otherButtonTitles:@"OK", nil];
    [alertView show];
    [self.tableView reloadData];
}

- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    PFUser *user = [self.friends objectAtIndex:[[self.tableView indexPathForSelectedRow] row]];
    NSLog(@"%@",user);
    PFRelation *friendsRelation = [[PFUser currentUser] relationForKey:@"friendsRelation"];

    if( 0 == buttonIndex ){ //cancel button

    } else if ( 1 == buttonIndex ){

        [friendsRelation removeObject:user];

        } else {

        }

        [[PFUser currentUser] saveInBackgroundWithBlock:^(BOOL succeed, NSError *error) {

            if (error) {
                NSLog(@"error %@ %@", error, [error userInfo]);
            } else {

            }

        }];

}

and at the moment all thats happening is the top row keeps getting deleted and not the row i select? Please help me get the row i select?

EDIT:

PFUser *user = [self.friends objectAtIndex:[[self.tableView indexPathForSelectedRow] row]];

This line is causing issue and not getting selected row?

I just needed to declare PFUser *user in header and use it as a global variable and then pass it in into [friendsRelation removeObject:]; as self.user.

Hope this helps other beginners.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM