我有一个应用程序,我通过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
是否在表视图中调用了reloadData
?您还应该使用-performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait
将消息发送到表视图。
如果解析器成功,您应该发送这些消息。
if (succeed)
{
[myTableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
}
最好调用表视图的数据源对象来更新它的数据存储然后告诉表视图更新它的数据