我正在尝试为我的应用实施“刷新”按钮。该应用程序解析Json文件并将数据放入表视图中。我想创建一个按钮,按下它时会尝试连接到服务器并获取更新的数据。 我到目前为止做了什么:
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem *button = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refresh)];
self.navigationItem.rightBarButtonItem = button;
我有一个名为refresh的方法:
-(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
如何使刷新按钮有效?
答案 0 :(得分:0)
首先创建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];
}
你可以尝试这样做,并确保你的代表和数据源已经连接,如果我做错了请不要让它
答案 1 :(得分:0)
当你致电
时-(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];
}
您应该注意,从服务器加载数据比重新加载表视图需要更多时间。我通常使用完成块来在完全获取数据时更新表,或者您可以使用委托方法。