我已经将代码放入下面,它看起来很接近,但我不明白为什么它没有进行实际的刷新。它有“拉动刷新”和正确显示更新的文本,但它没有更新实际数据。我是否遗漏了一些明显的东西,或者我放错了地方还是什么?
我编辑添加了 self TableView 调用以重新加载数据。仍然没有运气。
最终编辑___下面的用户通过调用数据提要解决了这个问题。
- (void)viewDidLoad
{
[super viewDidLoad];
UIRefreshControl *refresh = [[UIRefreshControl alloc] init];
refresh.attributedTitle = [[NSAttributedString alloc] initWithString:@"Pull to refresh"];
[refresh addTarget:self action:@selector(refreshmytable:) forControlEvents:UIControlEventValueChanged];
self.refreshControl = refresh;
NSURLSessionConfiguration *config =
[NSURLSessionConfiguration defaultSessionConfiguration];
_session = [NSURLSession sessionWithConfiguration:config
delegate:self
// delegate:nil
delegateQueue:nil];
[self fetchFeed];
}
- (void)refreshmytable:(UIRefreshControl *)refreshControl{
[self fetchFeed]; //Added 12:12 9.16.14
refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"Updating"];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"MMM d, h:mm a"];
NSString *updated = [NSString stringWithFormat:@" Last Update: %@", [formatter stringFromDate:[NSDate date]]];
refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:updated];
[refreshControl endRefreshing];
[self.tableView reloadData]; //Added this 11:32 9.16.14
}
- (void)fetchFeed
{
NSString *userEID = MAP_getUsername();
//NSLog(userEID);
NSString *requestString1 = [@"URL" stringByAppendingString:userEID];
NSString *requestString2 = @"&status=pending";
NSString *requestString = [requestString1 stringByAppendingString:requestString2];
//NSLog(requestString);
/*NSString *requestString = @"http://URL";
*/
NSURL *url = [NSURL URLWithString:requestString];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
NSURLSessionDataTask *dataTask =
[self.session dataTaskWithRequest:req
completionHandler:
^(NSData *data, NSURLResponse *response, NSError *error) {
NSDictionary *jsonObject = [NSJSONSerialization JSONObjectWithData:data
options:0
error:nil];
self.changeList = jsonObject[@"List"];
//self.changeList=nil; //tried to add here to remove duplicate data
NSLog(@"%@", self.changeList);
//- add code here to populate BNRItemStore with the change order list.
// - following code should be rewritten in fetchFeed that will load BNRItemStore.
if (self.changeList.count>0) {
for (int i = 0; i < self.changeList.count; i++) {
NSDictionary *coItem = self.changeList[i];
[[BNRItemStore sharedStore]
addItemWithApproverEid:coItem[@"approverEid"]
assignmentGroup:coItem[@"assignmentGroup"]
changeOrder:coItem[@"changeOrder"]
subcategory:coItem[@"subCatagory"]
title:coItem[@"title"]
];
}
}
//NSLog(@"sizeof(NSInteger) = %@", @(sizeof(NSInteger)));
//- end comment
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
//self.changeList=nil; //trying to null out list for refresh non duplicate data
// NSString *json = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
// NSLog(@"%@", json);
}];
[dataTask resume];
}
最佳答案
您没有获取新数据。您有一个在 viewDidLoad
中调用的方法/消息调用 fetchFeed
,但您从未在刷新方法/消息中调用它。我假设如果刷新,则需要获取新数据。调用“[self fetchFeed];”在重新加载 TableView 之前。如果您是异步获取数据,那么当获取新数据完成时,您需要在完成 block 中重新加载 TableView 。
关于ios - 下拉刷新功能不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25857687/