动态填充的表视图重新加载数据IOS

动态填充的表视图重新加载数据IOS,ios,json,refresh,Ios,Json,Refresh,我正在尝试为我的应用程序实现一个“刷新”按钮。该应用程序解析Json文件并将数据放入表视图中。我想制作一个按钮,当按下按钮时,它会尝试连接到服务器并获取更新的数据。 我到目前为止所做的: - (void)viewDidLoad { [super viewDidLoad]; UIBarButtonItem *button = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemRefr

我正在尝试为我的应用程序实现一个“刷新”按钮。该应用程序解析Json文件并将数据放入表视图中。我想制作一个按钮,当按下按钮时,它会尝试连接到服务器并获取更新的数据。 我到目前为止所做的:

- (void)viewDidLoad {
    [super viewDidLoad];


    UIBarButtonItem *button = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refresh)];

    self.navigationItem.rightBarButtonItem = button;
我有一个叫做刷新的方法:

-(void) refresh{
        [self.tableView reloadData];
}
但它似乎根本不起作用

我获取Json数据的方式如下:

    #import "JSONLoader.h"
#import "Location.h"
#import "Reachability.h"

@implementation JSONLoader


- (NSArray *)locationsFromJSONFile:(NSURL *)url {


    // Create a NSURLRequest with the given URL
    NSError *requestError = nil;
    NSURLRequest *request = [NSURLRequest requestWithURL:url
                                             cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
                                         timeoutInterval:30.0];

    // Get the data
    NSURLResponse *response;

    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&requestError];


    // Now create a NSDictionary from the JSON data
    NSDictionary *jsonDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

    // Create a new array to hold the locations
    NSMutableArray *locations = [[NSMutableArray alloc] init];

    // Get an array of dictionaries with the key "locations"
    NSArray *array = [jsonDictionary objectForKey:@"locations"];
    // Iterate through the array of dictionaries
    for(NSDictionary *dict in array) {
        // Create a new Location object for each one and initialise it with information in the dictionary
        Location *location = [[Location alloc] initWithJSONDictionary:dict];
        // Add the Location object to the array
        [locations addObject:location];
    }

    // Return the array of Location objects
    return locations;
    }



@end

如何使“刷新”按钮正常工作?

k然后您必须学习或了解如何使用“刷新”按钮的概念。如果没有使用网络连接,则它对检查非常有用。您可以基于此检查网络连接并刷新表

首先创建BOOL函数来检查互联网

+(BOOL)networkStatusAvailable{

    Reachability *internetReach =  [Reachability reachabilityForInternetConnection];
    [internetReach startNotifier];

    Reachability *wifiReach = [Reachability reachabilityForLocalWiFi] ;
    [wifiReach startNotifier];

    NetworkStatus netStatusInternetConnection = [internetReach currentReachabilityStatus];
    NetworkStatus netStatusLocalWiFi = [wifiReach currentReachabilityStatus];

    if(netStatusInternetConnection == NotReachable && netStatusLocalWiFi == NotReachable)
    {
        return netStatusInternetConnection;
    }
    else
    {
        return netStatusInternetConnection;
    }

}
希望您能正确地存储json数据。如何刷新意味着您可以做两件事:一件是直接按按钮重新加载,另一件是调用函数

然后检查bool并刷新表

    - (IBAction)refreshTable:(id)sender
    {

       if(networkStatusAvailable)
        {
       [self.tableView reloadData];
       }
       else
        {
          UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" 
                                                message:@"No network Connection" 
                                                delegate:self 
                                                cancelButtonTitle:@"OK" 
                                                otherButtonTitles:nil];
           [alert show];
        }

    }
否则

- (void)refreshTable
{
   if(networkStatusAvailable)
        {
        [self.tableView reloadData];
       }
       else
        {
          UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" 
                                                message:@"No network Connection" 
                                                delegate:self 
                                                cancelButtonTitle:@"OK" 
                                                otherButtonTitles:nil];
           [alert show];
        }
}


- (IBAction)refreshTable:(id)sender
{

   [self refreshTable];

}
您可以这样尝试,确保您的代表和数据源已连接,如果我有任何错误,请在您呼叫

-(void) refresh{
        [self.tableView reloadData];
}
方法它只刷新表视图,但表视图加载以前从服务器获取的数据。您需要再次调用从服务器获取数据的方法

您的刷新方法应该如下所示:

-(void) refresh
{
JSONLoader *aLoader = [JSONLoader alloc] init]; //init loader object
NSArray *newLocations = [aLoader locationsFromJSONFile:yourUrl]; //get the updated locations from server
self.oldLocations = newLocations; // assigning old array you used in table view datasource to the new data
[self.tableView reloadData];
}

您应该注意,从服务器加载数据比重新加载表视图花费更多的时间。我通常在完全获取数据时使用完成块来更新表,也可以使用委托方法

您在哪里调用刷新功能?您的数据是否包含数据?是的,它包含数据并按预期工作。问题是,当用户在没有internet连接的情况下打开应用程序时,数据不会被提取。当互联网处于活动状态时,它工作正常。我正在尝试的是,当用户再次连接internet时,重新连接刷新按钮并获取数据。是否要检查internet打开时自动更新tableview或当您点击按钮要重新加载时自动更新tableview?我检查可达性,并在没有internet时显示警报。我尝试了-(iAction)RefreshtTable:(id)sender{[tableView重新加载数据];}但是我得到了一个错误,选择器“reloadData”k的未知类方法,然后用UIAlertView代替NSLog显示,我更新了我的答案检查,你将tableView与委托和数据源连接了吗?很遗憾,我不能这样做,因为生成字典和将数据放入table view的过程在不同的文件中。和self。oldlocations=newLocations未被识别为JSONLoader对象。我试图做的是让这个“刷新”按钮从一开始就重新加载整个应用程序。不知何故,这是可能的吗?我尝试了abort(),但应用程序崩溃,我认为这不是正确的方法。不-不,你不需要重新启动应用程序。你需要刷新“oldlocations”在从表视图调用reloadData方法之前使用新位置数组。您需要#将“JSONLoader.h”导入表视图控制器,并使用locationsFromJSONFile方法刷新“oldlocations”数组。我相信你是这么做的。当你的应用程序启动时,你会在某个地方调用此方法一次,不是吗?然后你会将该方法返回到表视图控制器中的“oldlocations”数组中。因此,你应该在点击“刷新”按钮时再次调用该方法。