views:

237

answers:

1

Hi,

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: 

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?

Frank Schmitt
Hi, Right - I'm using reloadData and that it isn't interrupting the scrolling. I'll try your suggestions and let you know how I made out. Thanks for the quick response.
Vivas
Hi again. Okay, I got the problem fixed with your first suggestion. I'll try your other suggestions when time permits. For now, taking care of it in tableView:cellForRowAtIndexPath: does the trick. Thanks.
Vivas