Search code examples
iosuitableviewios7

TableView not reloading with new data from AFNetworking Response Object


Below I have code for a friends list tableview that has custom tableview cells that can be slided to the right to reveal 3 buttons. 1 of those buttons is the removeFriend button.

The button is supposed to delete the friend from the friend lists. And the table should then reload with the updated tableview that has the friend removed.
The deleted friend does not disapear from the tableview until I leave the view and then go back into the view.

The server is sending the updated JSON friend list data with the deleted friend removed. What exactly could I be doing wrong?

The code being used is below:

-(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];
}
}
}

Solution

  • The network request is asynchronous

    [friendsTableView reloadData];

    may be executed before the success block in which friendsList is updated