【问题标题】:TableView not reloading with new data from AFNetworking Response ObjectTableView 未使用来自 AFNetworking 响应对象的新数据重新加载
【发布时间】:2014-09-10 06:51:38
【问题描述】:

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

该按钮应该从好友列表中删除好友。然后表格应该重新加载已删除朋友的更新表格视图。
在我离开视图然后返回视图之前,已删除的朋友不会从 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];
}
}
}

【问题讨论】:

    标签: ios uitableview ios7


    【解决方案1】:

    网络请求是异步的

    [friendsTableView reloadData];

    可以在更新friendsList的成功块之前执行

    【讨论】:

    • 谢谢,我把代码改成了:else { friendsList = [NSMutableArray arrayWithArray:[(NSDictionary *)responseObject objectForKey:@"result"]]; [friendsTableView 重新加载数据]; } 现在可以使用了
    • @user3007683 很高兴我能提供帮助。顺便说一句,你最好在主线程中做reloadData。做到这一点 GCD 是一个不错的选择
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-24
    • 1970-01-01
    • 2011-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多