简体   繁体   中英

Reload UITableView

I have a class implementing the UITableViewDelegate and the NSXMLParserDelegate . My app is tab based. When I select the proper tab, in the viewWillAppear method of my class I start my xml parser to parse the rss feed at a specific url, then I populate my UITableView with the contents of the feeds.
Now I want to have a button to "refresh" the view (that is parse the rss feed again and display the new results). So, I have this method given as the action of the refresh button:

-(IBAction)refreshFeeds:(id)sender
{
    if (stories){
        [stories release];  // Stories is an NSMutableArray for storing the parsed feeds
        stories = nil;
    }
    [self viewWillAppear:YES];
    NSLog(@"RELOADING!!!!!!!!!!!!!!!");
}

My problem is that when i press the "refresh" button the view turns to blank, as if there are no feeds to display.
If then i switch to another tab an then come back, the tableview populates again with the feeds.
What am i doing wrong? Thank you in advance.

(Here is part of how i implemented the class)

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if ([stories count] == 0){
        NSString * path = @"http://www.theRssUrl.com";
        [self parseXMLFileAtURL:path];
    }
}
//custom method to parse rss xml
- (void)parseXMLFileAtURL:(NSString *)URL {

    if (stories) {
        [stories release];
        stories = nil;
    }
    stories = [[NSMutableArray alloc] init];
    [[NSURLCache sharedURLCache] setMemoryCapacity:0];
    [[NSURLCache sharedURLCache] setDiskCapacity:0];
    //you must then convert the path to a proper NSURL or it won't work
    NSURL *xmlURL = [NSURL URLWithString:URL];

    rssParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL];
    [rssParser setDelegate:self];

    [rssParser parse];
}
- (void)parserDidEndDocument:(NSXMLParser *)parser { 

    [newsTable reloadData]; //newsTable is the UITableView i want to refresh
    self.view = newsTable;
    [rssParser release];
    rssParser = nil;
}

EDIT:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   // Configure the cell.
   static NSString *MyIdentifier = @"MyIdentifier";
    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];

    if (cell == nil){
        cell = [[[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
    }

    int storyIndex = indexPath.row;

    cell.lTitle.text = [[stories objectAtIndex: storyIndex] objectForKey: @"title"];
    cell.lSummary.text = [[stories objectAtIndex: storyIndex] objectForKey: @"summary"];

    return cell;
}

You can call [self parseXMLFileAtURL:path]; directly to refresh your feed as this method empties the array. Don't call viewWillAppear directly. Just call parseXMLFileAtURL in viewWillAppear and refreshFeeds.

Calling viewWillAppear explicity is incorrect. You should rather have the reload logic in a separate function, call that function in viewWillAppear and on button press. Calling viewWillAppear in button press is something i dont think is correct.

As I understand your code, stories is the array that is providing the data for the newsTable . You are nil-ing it and updating the array. What if the newsTable requests data from its data source in between. It will find that stories to be empty and hence will have no data to show. We can assume there will a small but significant amount of delay in downloading and parsing your rss data. During this time, stories will remain empty.

You should consider downloading the rss data and parsing it into a different array. Replace the stories just before you are ready to refresh newsTable .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM