[英]Delete row from table view iOS Parse.com
I am trying to delete a selected row from my tableView with below code 我正在尝试使用以下代码从tableView中删除选定的行
-(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:];
我只需要在标头中声明PFUser * user并将其用作全局变量,然后将其传递给
[friendsRelation removeObject:];
as self.user. 作为self.user。
Hope this helps other beginners. 希望这对其他初学者有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.