拉到刷新功能不起作用

时间:2014-09-15 22:09:28

标签: ios objective-c

我已将下面的代码放入,但它看起来很接近,但我无法弄清楚它为什么没有进行实际刷新。它有"拉动刷新"并且更新的文本正确显示,但它没有更新实际数据。我错过了一些明显的东西,还是我把它放错了?

我编辑了添加自我tableview调用以重新加载数据。仍然没有运气。

下面的FINAL EDIT___用户通过调用数据源解决了这个问题。

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

2 个答案:

答案 0 :(得分:1)

您没有获取新数据。您有一个在fetchFeed中调用的方法/消息调用viewDidLoad,但您从未在刷新方法/消息中调用它。我假设如果你刷新,那么你需要获取新数据。拨打`[self fetchFeed];&#39;在重新加载表视图之前。如果要异步获取数据,那么在获取新数据时,您需要在完成块中重新加载表视图。

答案 1 :(得分:0)

你需要在reloadData方法的某个地方调用refreshmytable表视图来更新数据

- (void)refreshmytable:(UIRefreshControl *)refreshControl{
    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]

}