2011-04-08 30 views
1

我有一個應用程序,我通過NSXMLParser將我的XML數據加載到UITableView中。這一切都很完美。因爲我想添加一個ActivityIndi​​cator,所以我必須將我的加載數據放在不同的線程上,然後放在main中。在我做完這些之後,XML和我的應用程序一起被加載,但我在表格中看不到任何東西。當我點擊我的tabbarcontroller的不同選項卡,然後單擊回到表格中的表中的數據變得可見。出了什麼問題?XMLParser錯誤與NSThread

我的文件:

#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 

回答

0

你叫你的表視圖reloadData?您還應該使用-performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait將消息發送到表格視圖。

如果解析器成功,您應該發送這些消息。

if (succeed) 
{ 
    [myTableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO]; 
} 

它可能會更好打電話給你的表視圖的數據源對象更新它的數據存儲,然後從那裏,告訴表視圖,以更新其數據。

+0

否我應該放置這個reloadData和-performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait?對於愚蠢的問題抱歉。但我在這方面是一個新手。開始學習和閱讀書籍。歡迎所有幫助:-) – iJar 2011-04-08 14:22:59

+0

我已將它放在解析器成功消息下的代碼中。而我的dataReload方法在我的rootViewController中看起來像這樣: - (void)updateTable:(NSMutableArray *)data { \t appDelegate.stages = data; \t [self.tableView reloadData]; \t }現在,我的編譯器在數據加載的那一刻發生錯誤。 – iJar 2011-04-08 14:45:21

+0

那麼錯誤是什麼? – David 2011-04-08 14:48:36