I have a problem with removing unused pages from an array:
NSMutableArray *controllers = [[NSMutableArray alloc] init];
for (unsigned i = 0; i < [descriptionsList count]; i++) {
[controllers addObject:[NSNull null]];
}
self.viewControllers = controllers;
[controllers release];
[self loadScrollViewWithPage:0];
[self loadScrollViewWithPage:1];
i'm adding objects with:
- (void)loadScrollViewWithPage:(int)page {
if (page < 0) return;
if (page >= [descriptionsList count]) return;
// replace the placeholder if necessary
DetailsView *controller = [viewControllers objectAtIndex:page];
if ((NSNull *)controller == [NSNull null]) {
controller = [[DetailsView alloc] initWithElement:[descriptionsList objectAtIndex:page]
andFrame:CGRectMake(320*page, 0, 320, 420)];
[viewControllers replaceObjectAtIndex:page withObject:controller];
[controller release];
}
// add the controller's view to the scroll view
if (nil == controller.superview) {
[scrollView addSubview:controller];
}
}
and i'm using this to remove and create pages:
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
pageControlUsed = NO;
//load the visible page and the page on either side of it (to avoid flashes when the user starts scrolling)
for (unsigned i = 0; i < [descriptionsList count]; i++) {
if (i < pageController.currentPage - 1 || i > pageController.currentPage + 1) {
if ([viewControllers objectAtIndex:i] != nil) {
[[viewControllers objectAtIndex:i] release];
[viewControllers replaceObjectAtIndex:i withObject:[NSNull null]];
}
}
else {
[self loadScrollViewWithPage:i];
}
}
}
My app is crashing big time when i want to view page 3. Any advice on how should this be done? Thanks.