iPhone RSS 阅读器应用程序中的刷新问题

发布于 2024-11-19 13:24:56 字数 2641 浏览 2 评论 0原文

我正在研究我在网站上找到的这个教程。它是一个 RSS 源应用程序。链接如下: RSS 教程< /a>

我已经在我的应用程序中使用了 RSS Feed。我很好奇,没有使用普通的刷新按钮,而是选择了下拉刷新。 PullDownRefresh 教程

我在 RSS feed 中实现了这一点。一切看起来都不错并且可以工作,但只是不令人耳目一新。因此,我尝试添加一些代码,以便在拉下表格视图时使其刷新。

我输入的代码是:

- (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];
}

但这会重复提要。

在此之前我尝试使用 self.tableView.dataSource = nil; 清除数据源,但添加此操作会使其崩溃:

`- (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];
            }                            
        }];
    }        
}];
}`

输出窗口显示此信息:

由于未捕获的异常而终止应用程序 'NSInternalInconsistencyException',原因:'无效更新:无效 第 0 节中的行数。包含在第 0 节中的行数 更新后的现有部分 (0) 必须等于 更新之前该部分中包含的行 (0),加上或减去 从该部分插入或删除的行数(1 已插入, 0 已删除)。'

有什么想法吗?

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
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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

静若繁花 2024-11-26 13:24:56
- (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];

}
- (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];

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文