UISegmentedControl 与 UITableVIew NSRangeException

发布于 2024-08-26 10:37:35 字数 733 浏览 5 评论 0原文

我正在使用一个 UIViewController,如图所示:

@interface RssViewController : UIViewController <UITableViewDataSource,UITableViewDelegate,BlogRssParserDelegate>

我正在 UITableView(在 RssViewController 中)中显示 RSS 提要,具体取决于 UISegmentedControl 上选择的段。

当我滚动表格视图然后选择 UISegmentedControl 的另一个部分时,我的应用程序崩溃了。例如,默认情况下我有两个 RSS 提要,我在段 0 处显示 RSS 提要。该提要有 36 行。我在第 1 段加载的 RSS 提要只有 5 行。当我在第 0 段滚动 RSS 提要时,在滚动停止之前,我切换到第 1 段的 RSS 提要,导致应用程序崩溃,并出现以下错误:

* 由于未捕获的异常“NSRangeException”而终止应用程序,原因:“* -[NSCFArray objectAtIndex:]:索引 (36) 超出界限 (0)'

如果我等到段 0 处的 RSS 提要滚动停止,然后选择段 1,则一切正常。

我怎样才能阻止这种崩溃?我想重用相同的表视图,因为只有数据发生变化。我可以看到它由于行数而崩溃 - 我从 36 行减少到 5 行,但我该如何解决这个问题?

任何帮助/建议将不胜感激。

I am using one UIViewController as shown:

@interface RssViewController : UIViewController <UITableViewDataSource,UITableViewDelegate,BlogRssParserDelegate>

I am displaying an RSS feed in the UITableView (in RssViewController) depending on the segment selected on the UISegmentedControl.

My app crashes when I scroll the tableview then select another segment of the UISegmentedControl. For example I have two RSS feeds by default I am displaying the RSS feed at segment 0. This feed has 36 rows. The RSS feed that I load at segment 1 has only 5 rows. When I scroll the RSS feed at segment 0 THEN before the scrolling stops I switch to the RSS feed at segment 1 I crash the app with the following error:

* Terminating app due to uncaught exception 'NSRangeException', reason: '* -[NSCFArray objectAtIndex:]: index (36) beyond bounds (0)'

If I wait till the scrolling on the RSS feed at segment 0 stops THEN select segment 1, everything works fine.

How can I stop this crashing? I wanted to reuse the same tableview because only the data changes. I can see that it is crashing because of the row count - I went from 36 rows down to 5 rows BUT how can I fix this?

Any help / suggestions would be appreciated.

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

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

发布评论

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

评论(1

绝影如岚 2024-09-02 10:37:35

一种快速而肮脏的方法是检查您在 tableView:cellForRowAtIndexPath: 中查看的数组的边界,如果 indexPath.row 则返回一个空字符串。大于或等于[数组计数]

您还可以尝试在切换段时使用 deleteRowsAtIndexPaths:withRowAnimation: 或使用 scrollToRowAtIndexPath:atScrollPosition:animated: 显式滚动。我假设您已经在调用 reloadData 并且它不会中断滚动?

A quick-and-dirty approach would be to check the bounds of the array you're looking at in tableView:cellForRowAtIndexPath: and return an empty string if the indexPath.row is greater than or equal to [array count].

You could also try using deleteRowsAtIndexPaths:withRowAnimation: when switching segments or explicitly scroll using scrollToRowAtIndexPath:atScrollPosition:animated:. I'm assuming you're already calling reloadData and that it isn't interrupting the scrolling?

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