【问题标题】:UIRefreshControl isn't refreshing my XML table viewUIRefreshControl 没有刷新我的 XML 表视图
【发布时间】:2014-02-08 16:41:36
【问题描述】:

这是我的第一个 iOS 应用程序,非常感谢有关如何修复的详细说明。我正在尝试实现拉动刷新,以便我的 XML 表将刷新数据。目前,拉动刷新动画有效,但数据不刷新。

到目前为止,我收到的支持还不够详细,无法提供帮助。看来我快要让刷新工作了,但我被正式难住了。 NSLog 返回 ...2014-02-08 11:24:58.483 MyApp [3186:70b] 刷新。有人可以将修复粘贴到我的代码中吗?谢谢!

#import "MasterViewController.h"

#import "DetailViewController.h"

@interface MasterViewController () {
  NSXMLParser *parser;
  NSMutableArray *feeds;
  NSMutableDictionary *item;
  NSMutableString *title;
  NSMutableString *link;
  NSString *element;
}
@end

@implementation MasterViewController

- (void)awakeFromNib {
  [super awakeFromNib];
}

// Paste Blog feed here
- (void)viewDidLoad {
  [super viewDidLoad];
  feeds = [[NSMutableArray alloc] init];
  NSURL *url = [NSURL URLWithString:@"http://www.placeholder.xml"];
  parser = [[NSXMLParser alloc] initWithContentsOfURL:url];
  [parser setDelegate:self];
  [parser setShouldResolveExternalEntities:NO];
  [parser parse];
  UIRefreshControl *refreshControl = [UIRefreshControl new];
  [refreshControl addTarget:self
                 action:@selector(refresh:)
       forControlEvents:UIControlEventValueChanged];
  refreshControl.attributedTitle =
  [[NSAttributedString alloc] initWithString:@"Pull to refresh..."];
  self.refreshControl = refreshControl;
}

- (void)refresh:(UIRefreshControl *)sender {
  // ... your refresh code
NSLog(@"refreshing...");
  NSURL *url = [NSURL URLWithString:@"http://www.placeholder.xml"];
  NSURLRequest *request = [NSURLRequest requestWithURL:url];
  (void)[[NSURLConnection alloc] initWithRequest:request delegate:self];

  [sender endRefreshing];
}

- (void)didReceiveMemoryWarning {
  [super didReceiveMemoryWarning];
}

#pragma mark - Table View

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  return 1;
}

- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section {
  return feeds.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView
     cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  UITableViewCell *cell =
      [tableView dequeueReusableCellWithIdentifier:@"Cell"
                                  forIndexPath:indexPath];
  cell.textLabel.text =
      [[feeds objectAtIndex:indexPath.row] objectForKey:@"title"];
  return cell;
}

- (void)parser:(NSXMLParser *)parser
didStartElement:(NSString *)elementName
   namespaceURI:(NSString *)namespaceURI
  qualifiedName:(NSString *)qName
     attributes:(NSDictionary *)attributeDict {

  element = elementName;

  if ([element isEqualToString:@"item"]) {

item = [[NSMutableDictionary alloc] init];
title = [[NSMutableString alloc] init];
link = [[NSMutableString alloc] init];
  }
}

- (void)parser:(NSXMLParser *)parser
didEndElement:(NSString *)elementName
 namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName {

  if ([elementName isEqualToString:@"item"]) {

[item setObject:title forKey:@"title"];
[item setObject:link forKey:@"link"];

[feeds addObject:[item copy]];
  }
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {

  if ([element isEqualToString:@"title"]) {
[title appendString:string];
  } else if ([element isEqualToString:@"link"]) {
[link appendString:string];
  }
}

- (void)parserDidEndDocument:(NSXMLParser *)parser {

  [self.tableView reloadData];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  if ([[segue identifier] isEqualToString:@"showDetail"]) {

NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSString *string = [feeds[indexPath.row] objectForKey:@"link"];
[[segue destinationViewController] setUrl:string];
  }
}

@end

【问题讨论】:

  • 你的 NSURLConnection 代表在哪里?你也使用相同的 url,那么你将如何获得不同的数据?
  • 你的代码真的到达了 refresh: 方法吗?放一个 NSLog 让我们知道。如果不是,那就是刷新控制问题。如果调用 refresh: ,那么您的 NSURLConnection 方法有问题,正如 Midhun 所说。也许在 iOS7 中一个更好的选择是现在 NSURLSession 带有很好的完成处理程序,所以你可以把你所有的更新代码放在那里。
  • 它没有刷新数据。我只得到刷新动画的拉动。你能把修复粘贴到我的代码中吗?就像我提到的,这是我的第一个应用程序。
  • 如果你把 NSLog(@"refreshing...");就在 // ...您的刷新代码行下方,您是否在控制台中收到“正在刷新...”消息?
  • 这是 NSLog 返回的内容...2014-02-08 11:24:58.483 MyApp [3186:70b] 刷新...

标签: ios objective-c xml uitableview


【解决方案1】:

一旦用户在您的 tableview 上拉动刷新,就会触发此代码:

- (void)refresh:(UIRefreshControl *)sender {
  // ... your refresh code
  NSURL *url = [NSURL URLWithString:@"http://www.placeholder.xml"];
  NSURLRequest *request = [NSURLRequest requestWithURL:url];
  (void)[[NSURLConnection alloc] initWithRequest:request delegate:self];

  [sender endRefreshing];
}

您在这里所做的是告诉刷新控件在用户开始刷新后立即结束刷新。

在您的 URLConnection/parsing/refresh 操作完成之前,您不应该调用 -endRefreshing

【讨论】:

  • 那么我应该把 endRefreshing 移到哪里?
  • @user3251476 好吧,既然您正在执行网络请求作为刷新操作的一部分,那么您的网络请求何时完成呢?或者如果您要处理请求的结果(解析它,无论如何),您可以在 that 完成时执行它。
【解决方案2】:

你可以用这个替换你的 refresh: 方法。它基于新的 NSURLSession 方法,您应该将完成处理程序主体中的 cmets 替换为您的调用以处理您的 xml 数据并更新您的表。

- (void)refresh:(UIRefreshControl *)sender
{
    // ... your refresh code
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];

    NSURLSession *session = [NSURLSession sharedSession];
    [[session dataTaskWithURL:[NSURL URLWithString:@"http://www.placeholder.xml"]
            completionHandler:^(NSData *data,
                                NSURLResponse *response,
                                NSError *error) {

                // handle response
                NSXMLParser *updatedParser = [[NSXMLParser alloc] initWithData:data];
                [updatedParser parse];

                [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
            }] resume];
    [sender endRefreshing];
}

【讨论】:

  • 那么插入此代码应该可以解决我的问题吗?您提到我应该替换完成处理程序主体中的 cmets ...您能否提供更多详细信息?非常感谢马丁的帮助。
  • 我没有使用 xml 解析器的经验,但根据您的代码猜测,只需加载数据并要求再次解析就足够了。我已经更新了代码。
  • 不幸的是,这仍然不起作用......还有其他想法吗?感谢您的帮助。
猜你喜欢
  • 2023-04-01
  • 2013-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多