TableView无法使用AFNetworking Response Object中的新数据重新加载

时间:2014-09-10 06:51:38

标签: ios uitableview ios7

下面我有一个朋友列表tableview的代码,它有自定义的tableview单元格,可以向右滑动以显示3个按钮。其中一个按钮是removeFriend按钮。

按钮应该从朋友列表中删除朋友。然后应该使用已删除朋友的更新的tableview重新加载表。
删除的朋友在离开视图然后返回视图之前不会从tableview中消失。

服务器正在发送更新的 JSON 好友列表数据,删除已删除的好友。我究竟能做错什么?

使用的代码如下:

-(void) deleteAction:(id) sender
{
UIButton * btn = (UIButton *) sender;  // delete friend

NSLog(@"%d", btn.tag);

self.dic = [friendsList objectAtIndex:btn.tag];

NSString * friendID = [self.dic objectForKey:@"id"];

NSLog(@"rejected ID : %@", friendID);
deleteAlert=[[UIAlertView alloc]initWithTitle:@"Friend Removal Confirmation" message:@"\n\nAre you sure you want to remove this friend\n\n\n\n" delegate:self cancelButtonTitle:@"NO" otherButtonTitles:@"YES", nil];
[deleteAlert show];


}


-(void) deleteAction2
{

NSString * friendID = [self.dic objectForKey:@"id"];

[self removeFriend:friendID];

}   

-(void) removeFriend:(NSString *) friendId
{
NSString * userID = [[NSUserDefaults standardUserDefaults]  objectForKey:USERID];

NSMutableDictionary* params =[NSMutableDictionary dictionaryWithObjectsAndKeys:@"removeFriend", @"command", friendId,@"Requesterid", userID/*friendId*/, @"userid", nil];
NSLog( @"%@", params);
[SVProgressHUD showWithStatus:@"Loading..." maskType:SVProgressHUDMaskTypeBlack];

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager POST:BaseURLString parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {

    NSLog(@"JSON: %@", responseObject);

    [SVProgressHUD dismiss];

    NSString * error = [responseObject objectForKey:@"error"];

    if(error)
        [[[UIAlertView alloc] initWithTitle:@"Error" message:error delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil] show];
    else
    {
        friendsList = [NSMutableArray arrayWithArray:[(NSDictionary *)responseObject objectForKey:@"result"]];



    }
    //        else
    //            [[[UIAlertView alloc] initWithTitle:@"Alert" message:@"You sent request." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil] show];


} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

    [SVProgressHUD dismiss];

    NSLog(@"Error: %@", error);
}];
  [friendsTableView reloadData];
}

#pragma alertview delegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if(alertView == addFriendAlert)
{

    if(buttonIndex == 1)
    {
        addFriendId = [addFriendAlert textFieldAtIndex:0].text;

        if([addFriendId isEqualToString:@""])
            return;

        NSLog(@"%@", addFriendId);

        [self sendingFriendAddRequest:addFriendId];
    }
}


if(alertView == deleteAlert){

if([title isEqualToString:@"NO"])
{
    NSLog(@"Nothing to do here");
}
else if([title isEqualToString:@"YES"])
{
    NSLog(@"Delete the cell");
    [self deleteAction2];
}
}
}

1 个答案:

答案 0 :(得分:0)

网络请求是异步的

  

[friendsTableView reloadData];

可以在friendsList更新

的成功块之前执行