为什么不是';我的iOS刷新控制器不能工作吗?

为什么不是';我的iOS刷新控制器不能工作吗?,ios,objective-c,Ios,Objective C,有人能解释为什么我的“拉刷新”功能不起作用吗?我调用了UIRefresh控制器,“拉动刷新”动画可以工作,但表视图不会重新加载XML数据。我做错了什么?请帮忙 #import "MasterViewController.h" #import "DetailViewController.h" @interface MasterViewController () { NSXMLParser *parser; NSMutableArray *feeds; NSMutableDictio

有人能解释为什么我的“拉刷新”功能不起作用吗?我调用了UIRefresh控制器,“拉动刷新”动画可以工作,但表视图不会重新加载XML数据。我做错了什么?请帮忙

#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
  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

此处答案中提出的想法应有助于:


基本上创建UIRefreshControl,然后将其作为子视图添加到任何UIScrollView。仅供参考UITableView是UIScrollView的子类

您需要定义“不工作”。什么不工作?您是否将其添加到视图中?您的视图控制器是UITableViewController的子类吗?