2014-02-09 10 views
0

这是我的第一个应用程序,我试图实现拉刷新控制。我碰到了路障。有人可以识别和修复我的代码吗?拉来刷新动画的作品,但我的表中的XML数据不会刷新。先谢谢你。有人可以识别和修复我的iOS拉刷新代码?

#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://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; 
    [self.tableView reloadData]; 

} 

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

    NSURLSession *session = [NSURLSession sharedSession]; 
    [[session dataTaskWithURL:[NSURL URLWithString:@"http://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]; 
} 

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

回答

1

你需要告诉你的UITableView什么时候应该刷新自己。目前你下载新的数据,但你没有做任何事情。

如果您只希望加载特定的行/段范围,您可能需要在表视图上调用-reloadData方法,或者使用其他重载方法之一。在完成分析已下载的新数据并准备好显示它之后,您应该调用它。

+0

lxt,你能告诉我我的代码应该看起来如何吗? – user3251476

+1

lxt在添加'[self.tableView reloadData];'在你的NSURLSession的'completionHandler'(在'refresh:'方法中)的末尾。看起来你还需要添加'[updatedParser setDelegate:self];' –

+0

谢谢你们的继续帮助。我添加了[self.tableView reloadData];但是当我尝试添加[updatedParser setDelegate:self];它弹出一个红色的错误。 – user3251476

相关问题