开发者

UISegmentedControl with UITableVIew NSRangeException

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.


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜