NSThread 的 XMLParser 错误
我有一个应用程序,在其中我通过 NSXMLParser 将 XML 数据加载到 UITableView 中。这一切都很完美。因为我想添加一个 ActivityIndicator,所以我必须将加载数据放在与主线程不同的线程上。执行此操作后,XML 以及我的应用程序都被加载,但我在表中看不到任何内容。当我单击选项卡控制器的不同选项卡,然后单击返回表时,表中的数据变得可见。出了什么问题?
我的文件:
#import "DAFAppDelegate.h"
#import "RootViewController.h"
#import "XMLParser.h"
@implementation DAFAppDelegate
@synthesize window;
@synthesize navigationController;
@synthesize rootViewController;
@synthesize rootTabController;
@synthesize stages;
+ (void) showAlert
{
UIAlertView *av = [[[UIAlertView alloc] initWithTitle:@"No Connection" message:@"Could not retrieve data" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease];
[av show];
}
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
[window addSubview:[rootTabController view]];
[window makeKeyAndVisible];
[NSThread detachNewThreadSelector:@selector(parseXML) toTarget:self withObject:nil];
}
- (void) parseXML
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
NSURL *url = [[NSURL alloc] initWithString:@"http://web.me.com/ijar/Stages.xml"];
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];
//Initialize the delegate.
XMLParser *parser = [[XMLParser alloc] initXMLParser];
//Set delegate
[xmlParser setDelegate:parser];
//Start parsing the XML file.
BOOL success = [xmlParser parse];
if(success)
{
NSLog(@"No Errors");
}
else
{
[DAFAppDelegate showAlert];
NSLog(@"Error Error Error!!!");
}
[pool release];
}
- (void)dealloc
{
[navigationController release];
[rootViewController release];
[rootTabController release];
[window release];
[stages release];
[super dealloc];
}
@end
I have an application in which I load my XML data via the NSXMLParser into an UITableView. This all worked perfectly. Because I wanted to add a ActivityIndicator I have to place my loading data on a different thread then the main. After I did this the XML get loaded as well as my application but I see nothing in my table. When I click on a different tab of my tabbarcontroller and then click back to the table the data in the table becomes visible. What is going wrong?
My file:
#import "DAFAppDelegate.h"
#import "RootViewController.h"
#import "XMLParser.h"
@implementation DAFAppDelegate
@synthesize window;
@synthesize navigationController;
@synthesize rootViewController;
@synthesize rootTabController;
@synthesize stages;
+ (void) showAlert
{
UIAlertView *av = [[[UIAlertView alloc] initWithTitle:@"No Connection" message:@"Could not retrieve data" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease];
[av show];
}
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
[window addSubview:[rootTabController view]];
[window makeKeyAndVisible];
[NSThread detachNewThreadSelector:@selector(parseXML) toTarget:self withObject:nil];
}
- (void) parseXML
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
NSURL *url = [[NSURL alloc] initWithString:@"http://web.me.com/ijar/Stages.xml"];
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];
//Initialize the delegate.
XMLParser *parser = [[XMLParser alloc] initXMLParser];
//Set delegate
[xmlParser setDelegate:parser];
//Start parsing the XML file.
BOOL success = [xmlParser parse];
if(success)
{
NSLog(@"No Errors");
}
else
{
[DAFAppDelegate showAlert];
NSLog(@"Error Error Error!!!");
}
[pool release];
}
- (void)dealloc
{
[navigationController release];
[rootViewController release];
[rootTabController release];
[window release];
[stages release];
[super dealloc];
}
@end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否在表格视图上调用了
reloadData
?此外,您还应该使用 -performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait 将消息发送到表视图。如果您的解析器成功,您应该发送这些消息。
最好调用表视图的数据源对象来更新其数据存储,然后从那里告诉表视图更新其数据。
Did you call
reloadData
on your table view? Also you should send the message to the table view using-performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait
.You should send these messages if your parser succeeded.
It might be better to call your table view's datasource object to update it's data store and then from there, tell the table view to update its data.