【发布时间】: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