下面的代码通过获取解析XML并将其放入视图的Google新闻RSS源将数据加载到UITableView中。它工作,但当我推动另一个视图并返回表视图滚动被打破。我已将问题与GCD代码隔离开来。如果我删除它,问题就会消失。所以这是GCD代码:
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"View did load ..");
self.title = @"News stories";
NewsItem *item = [[NewsItem alloc] init];
item.title = @"Loading ...";
self.newsItems = [@[item] mutableCopy];
NSString *URL = @"http://news.google.com/news?q=apple+OR+google+OR+microsoft&output=rss";
NSURL *xmlURL = [NSURL URLWithString:URL];
NSURLRequest *request = [NSURLRequest requestWithURL:xmlURL];
Parser *parser = [[Parser alloc] initXMLParser];;
dispatch_queue_t downloadQueue = dispatch_queue_create("news downloader", NULL);
dispatch_async(downloadQueue, ^{
BOOL success;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
if (data != nil) {
NSXMLParser *nsXmlParser = [[NSXMLParser alloc] initWithData:data];
// create and init our delegate
// set delegate
[nsXmlParser setDelegate:parser];
// parsing...
success = [nsXmlParser parse];
}
else {
success = FALSE;
}
// test the result
dispatch_async(dispatch_get_main_queue(), ^{
if (success) {
NSLog(@"reloading data ...");
self.newsItems = [parser.newsItems copy];
[self.tableView reloadData];
} else {
NewsItem *item = [[NewsItem alloc] init];
item.title = @"Error loading";
self.newsItems = [@[item] mutableCopy];
[self.tableView reloadData];
NSLog(@"Error parsing document!");
}
});
});
}
答案 0 :(得分:2)
在[self.tableView reloadData];
中尝试viewDidAppear
。