这是我的第一个iOS应用程序,所以如此详细的说明将非常感谢。我正在尝试实现一个pull来刷新,以便我的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
答案 0 :(得分:0)
一旦用户在您的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 /解析/刷新操作完成之前,不应该调用-endRefreshing
。
答案 1 :(得分:0)
您可以使用此替换refresh:方法。它基于新的NSURLSession方法,你应该用你的调用替换完成处理程序正文中的注释来处理你的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];
}