2011-04-06 35 views
1

我在學習IOS開發,我從一個示例RSS源應用程序創建了一個項目,該應用程序從我的服務器上的XML源文件加載數據。這工作正常,但我希望它刷新,如果主頁按鈕被按下(ios多任務)。我已經嘗試了[table reloaddata]並將其放入所有viewdidload/viewdiddisappear部分,但它不能正常工作,並且它不會停留在我將它們全部放置的斷點上。noobie表刷新問題

@implementation RootViewController 

- (void)viewDidLoad { 
    // Add the following line if you want the list to be editable 
    // self.navigationItem.leftBarButtonItem = self.editButtonItem; 
    //[newsTable reloaddata]; 
} 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return [stories count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *MyIdentifier = @"MyIdentifier"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease]; 
    } 

    // Set up the cell 
    int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1]; 
    [cell setText:[[stories objectAtIndex: storyIndex] objectForKey: @"title"]]; 

    return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    // Navigation logic 

    int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1]; 

    NSString * storyLink = [[stories objectAtIndex: storyIndex] objectForKey: @"link"]; 

    // clean up the link - get rid of spaces, returns, and tabs... 
    storyLink = [storyLink stringByReplacingOccurrencesOfString:@" " withString:@""]; 
    storyLink = [storyLink stringByReplacingOccurrencesOfString:@"\n" withString:@""]; 
    storyLink = [storyLink stringByReplacingOccurrencesOfString:@" " withString:@""]; 

    NSLog(@"link: %@", storyLink); 
    // open in Safari 
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:storyLink]]; 
} 

- (void)viewWillAppear:(BOOL)animated { 
    [super viewWillAppear:animated]; 
    [newsTable reloadData]; 
} 

- (void)viewDidAppear:(BOOL)animated { 
    [super viewDidAppear:animated]; 
    if ([stories count] == 0) { 
     [newsTable reloadData]; 
     NSString * path = @"http://www.myserver.co.uk/test.xml"; 
     [self parseXMLFileAtURL:path]; 
     //[NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(reloadData) userInfo:nil repeats:FALSE]; 
    } 

    cellSize = CGSizeMake([newsTable bounds].size.width, 60); 
} 

-(void)viewWillDisappear:(BOOL)animated { 
    [newsTable reloadData]; 
} 

- (void)viewDidDisappear:(BOOL)animated { 
    [newsTable reloadData];} 

- (void)applicationDidBecomeActive:(UIApplication *)application { 
    /* 
    Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 
    */ 
    [newsTable reloadData]; 
} 

- (void)applicationWillEnterForeground:(UIApplication *)application { 
    /* 
    Called as part of transition from the background to the inactive state: here you can undo many of the changes made on entering the background. 
    */ 
    [newsTable reloadData]; 
} 

- (void)parserDidStartDocument:(NSXMLParser *)parser{ 
    NSLog(@"found file and started parsing"); 
} 

- (void)parseXMLFileAtURL:(NSString *)URL 
{ 
    stories = [[NSMutableArray alloc] init]; 

    //you must then convert the path to a proper NSURL or it won't work 
    NSURL *xmlURL = [NSURL URLWithString:URL]; 

    // here, for some reason you have to use NSClassFromString when trying to alloc NSXMLParser, otherwise you will get an object not found error 
    // this may be necessary only for the toolchain 
    rssParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL]; 

    // Set self as the delegate of the parser so that it will receive the parser delegate methods callbacks. 
    [rssParser setDelegate:self]; 

    // Depending on the XML document you're parsing, you may want to enable these features of NSXMLParser. 
    [rssParser setShouldProcessNamespaces:NO]; 
    [rssParser setShouldReportNamespacePrefixes:NO]; 
    [rssParser setShouldResolveExternalEntities:NO]; 

    [rssParser parse]; 
} 

- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError { 
    NSString * errorString = [NSString stringWithFormat:@"Unable to download story feed from web site (Error code %i)", [parseError code]]; 
    NSLog(@"error parsing XML: %@", errorString); 

    UIAlertView * errorAlert = [[UIAlertView alloc] initWithTitle:@"Error loading content" message:errorString delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
    [errorAlert show]; 
} 

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{    
    //NSLog(@"found this element: %@", elementName); 
    currentElement = [elementName copy]; 
    if ([elementName isEqualToString:@"item"]) { 
     // clear out our story item caches... 
     item = [[NSMutableDictionary alloc] init]; 
     currentTitle = [[NSMutableString alloc] init]; 
     currentDate = [[NSMutableString alloc] init]; 
     currentSummary = [[NSMutableString alloc] init]; 
     currentLink = [[NSMutableString alloc] init]; 
    } 
} 

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{  
    //NSLog(@"ended element: %@", elementName); 
    if ([elementName isEqualToString:@"item"]) { 
     // save values to an item, then store that item into the array... 
     [item setObject:currentTitle forKey:@"title"]; 
     [item setObject:currentLink forKey:@"link"]; 
     [item setObject:currentSummary forKey:@"summary"]; 
     [item setObject:currentDate forKey:@"date"]; 

     [stories addObject:[item copy]]; 
     NSLog(@"adding story: %@", currentTitle); 
    } 
} 

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{ 
    //NSLog(@"found characters: %@", string); 
    // save the characters for the current item... 
    if ([currentElement isEqualToString:@"title"]) { 
     [currentTitle appendString:string]; 
    } else if ([currentElement isEqualToString:@"link"]) { 
     [currentLink appendString:string]; 
    } else if ([currentElement isEqualToString:@"description"]) { 
     [currentSummary appendString:string]; 
    } else if ([currentElement isEqualToString:@"pubDate"]) { 
     [currentDate appendString:string]; 
    } 
} 

- (void)parserDidEndDocument:(NSXMLParser *)parser { 

    [activityIndicator stopAnimating]; 
    [activityIndicator removeFromSuperview]; 

    NSLog(@"all done!"); 
    NSLog(@"stories array has %d items", [stories count]); 
    [newsTable reloadData]; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    // Return YES for supported orientations 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview 
    // Release anything that's not essential, such as cached data 
} 

- (void)dealloc { 

    [currentElement release]; 
    [rssParser release]; 
    [stories release]; 
    [item release]; 
    [currentTitle release]; 
    [currentDate release]; 
    [currentSummary release]; 
    [currentLink release]; 

    [super dealloc]; 
} 

@end 

回答

3

我喜歡使用通知來處理這種情況。這行代碼添加到你的「viewDidLoad中」的方法

[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(updateRSSFeed) 
              name:UIApplicationWillEnterForegroundNotification 
              object:nil]; 

的「updateRSSFeed」的方法會做這樣的事情

- (void) updateRSSFeed 
{ 
    NSLog(@"Feed Me!!!"); 
    // Code to restart loading the data from the RSS feed and ultimately reloading 
    // the table view. 
} 

而且不要忘記刪除觀察者所以這增加了「的dealloc」方法

[[NSNotificationCenter defaultCenter] removeObserver: self]; 

在具有多個視圖控制器一個複雜的應用程序視圖控制器可以在不應用程序退出被解除分配和通知可以被髮送到一個不存在的實例你會崩潰。

通知非常有用,因此深入研究文檔和其他來源以瞭解如何使用它們。

最後,方法'applicationDidBecomeActive:'和'applicationWillEnterForeground:'將永遠不會在根視圖控制器中調用,因爲它們是appDelegate遵守的應用程序委託協議的一部分。

+0

輝煌的謝謝,stackoverflow 0wnes! – arfur 2011-04-08 21:37:43

0

嘿,如果你想在應用程序進入後臺反應,你應該實現

- (void)applicationDidEnterBackground:(UIApplication *)application 
您的appDelegate

,並從那裏觸發數據重新加載。 viewWillAppear中,當應用程序將會背景(不會被調用,即當你「最小化」了,按Home鍵,或者切換到其他應用程序。

乾杯

0

我真不明白你在哪裏根據你的描述,你必須在applicationWiilResignActive:期間調用它,但是這很可能不會運行,除非你已經向操作系統表明你想在後臺下載。

但是我相信你必須有已經啓動下載這個方法之前調用,這將解釋爲什麼你看不到的表格重新加載或調用斷點。

這可能是我誤解你是怎麼做的,但我認爲你有一個基本的設計問題,因爲你不能在停用時重新加載開始