【问题标题】:Delete row from table view iOS Parse.com从表视图中删除行 iOS Parse.com
【发布时间】:2016-01-17 13:06:32
【问题描述】:

我正在尝试使用以下代码从我的 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]];

此行导致问题并且未获得选定行?

【问题讨论】:

  • 既然你是deselectRowAtIndexPath,你怎么期待indexPathForSelectedRow返回任何有用的东西?
  • @luk2302 删除deselectRowAtIndexPath后我仍然遇到同样的问题

标签: ios objective-c uitableview parse-platform pfquery


【解决方案1】:

我只需要在标头中声明 PFUser *user 并将其用作全局变量,然后将其作为 self.user 传递给[friendsRelation removeObject:];

希望这对其他初学者有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多