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.
-(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
.
精彩评论