Problem with refresh in iPhone RSS reader app
I am playing around with this tutorial I found on a website. It is an RSS Feed application. Here's the link: RSS Tutorial
I have got the RSS Feed working in my application. I got curious and instead of having a normal refresh button, I opted for a pull down refresh. PullDownRefresh Tutorial
I implemented into the RSS feed. All looks good and working but just not refreshing. So I tried adding some code in in order to make it refresh when I pull the tableview down.
The code I put is:
- (void)reloadTableViewDataSource {
//should be calling your tableviews data source model to reload.
//put here just for demo.
[self performSelector:@selector(refresh)];
_reloading = YES;
[self.tableView reloadData];
}
But this duplicates the feeds.
I tried to clear my datasource prior to this with self.tableView.dataSource = nil;
but adding this crashes it here:
`- (void)requestFinished:(ASIHTTPRequest *)request {
[_queue addOperationWithBlock:^{
NSError *error;
GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:[request responseData]
options:0 error:&error];
if (doc == nil) {
NSLog(@"Failed to parse %@", request.url);
} else {
NSMutableArray *entries = [NSMutableArray array];
[self parseFeed:doc.rootElement entries:entries];
NSSortDescriptor *itemSort = [[NSSortDescriptor alloc] initWithKey:@"articleDate" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:itemSort,nil];
[entries sortUsingDescriptors:sortDescriptors];
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
for (RSSEntry *entry in entries) {
int insertIdx = [_allEntries indexForInsertingObject:entry sortedUsingBlock:^(id a, id b) {
RSSEntry *entry1 = (RSSEntry *) a;
RSSEntry *entry2 = (RSSEntry *) b;
return [entry1.articleDate compare:entry2.articleDate];
}];
[_allEntries insertObject:entry atIndex:insertIdx];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:insertIdx inSection:0]] //Thread 1: Program received signal: "SIGABRT".
withRowAnimation:UITableViewRowAnimationRight];
}
}];
}
}];
}`
The output window says this:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an 开发者_如何学Python existing section after the update (0) must be equal to the number of rows contained in that section before the update (0), plus or minus the number of rows inserted or deleted from that section (1 inserted, 0 deleted).'
Any ideas?
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationController.navigationBar.tintColor = [UIColor blackColor];
self.allEntries = [NSMutableArray array];
self.title= NSLocalizedString(@"News feeds", @"Storage");
self.queue = [[[NSOperationQueue alloc] init ] autorelease];
self.feeds = [NSArray arrayWithObjects:
@"feed://www.yourfeed.com/feed.xml",
nil];
[self.tableView reloadData];
// Add the right reload button
UIBarButtonItem *reloadButton = [[UIBarButtonItem alloc]
initWithTitle:NSLocalizedString(@"Refresh")
style:UIBarButtonItemStyleBordered
target:self
action:@selector(viewDidLoad)];
self.navigationItem.rightBarButtonItem = reloadButton;
self.navigationItem.rightBarButtonItem.style = UIBarButtonItemStyleDone;
[reloadButton release];
[self refresh];
}
精彩评论