[英]Delete row from table view iOS Parse.com
我正在尝试使用以下代码从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 {
}
}];
}
而目前所有发生的事情是第一行不断被删除,而不是我选择的行吗? 请帮助我获得我选择的行?
编辑:
PFUser *user = [self.friends objectAtIndex:[[self.tableView indexPathForSelectedRow] row]];
此行引起问题,但没有得到选择的行?
我只需要在标头中声明PFUser * user并将其用作全局变量,然后将其传递给[friendsRelation removeObject:];
作为self.user。
希望这对其他初学者有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.