下面我有一个好友列表表格视图的代码,该表格视图具有自定义表格视图单元格,可以向右滑动以显示 3 个按钮。其中 1 个按钮是“removeFriend”按钮。
该按钮应该是从好友列表中删除好友。然后,该表应该使用已删除好友的更新后的表视图重新加载。
删除的朋友不会从桌面视图中消失,直到我离开视图然后返回视图。
服务器正在发送更新的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];
}
}
}