【问题标题】:Dynamically Populated Table view Reload Data IOS动态填充的表视图重新加载数据 IOS
【发布时间】:2014-03-31 03:18:58
【问题描述】:

我正在尝试为我的应用程序实现“刷新”按钮。该应用程序解析 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

如何使刷新按钮起作用?

【问题讨论】:

  • 你在哪里调用你的刷新函数?
  • 你的数据中是否包含数据??
  • 是的,它包含数据并且按预期工作。问题是当用户在没有互联网连接的情况下打开应用程序时,不会获取数据。当互联网处于活动状态时,它可以正常工作。我要做的是让刷新按钮重新连接并在用户再次上网时获取数据。
  • 您想检查是否在互联网开启或点击按钮时自动更新您的表格视图想要重新加载?

标签: ios json refresh


【解决方案1】:

当你调用

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

您应该注意,从服务器加载数据比重新加载表格视图需要更多时间。我通常在数据完全提取时使用完成块来更新表,或者您可以使用委托方法。

【讨论】:

  • 遗憾的是我不能这样做,因为生成字典并将数据放入表视图中的过程在不同的文件中。并且 self.oldlocations = newLocations 不被识别为 JSONLoader 对象。我想做的是让这个“刷新”按钮从一开始就重新加载整个应用程序。有可能吗?我尝试了 abort() 但应用程序崩溃了,我认为它不是正确的方法。
  • 不,您不需要重新启动应用程序。在从表视图调用 reloadData 方法之前,您需要用新位置刷新“oldlocations”数组。您需要将“JSONLoader.h”#import 到表视图控制器并使用 locationsFromJSONFile 方法刷新“oldlocations”数组。我相信你做到了。当您的应用程序启动时,您会在某个地方调用此方法一次,不是吗?然后将方法返回分配给表视图控制器中的“oldlocations”数组。所以你应该再次点击“刷新”按钮。
【解决方案2】:

k 那么你必须学习或知道如何使用Reachability 概念,如果网络连接没有使用它对 chackin 很有用,你可以根据你可以检查和刷新表来检查网络连接。

首先创建 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];

}

你可以这样尝试,确保你的代表和数据源已经连接,如果我做错了,请不要让我这样做

【讨论】:

  • 我检查可达性,并在没有互联网时显示警报。我试过 - (IBAction)refreshTable:(id)sender{ [tableView reloadData];} 但我收到一个错误 No known class method for选择器'reloadData'
  • k 然后你使用 UIAlertView 显示的不是 NSLog 我更新了我的答案检查
  • 您是否已将 tableView 与委托和数据源连接?
猜你喜欢
  • 2011-05-01
  • 2016-07-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-02
  • 1970-01-01
  • 2016-06-08
  • 2013-08-21
相关资源
最近更新 更多