在我的分页滚动视图上需要一些复杂问题的帮助。我添加了多个带有tableviews的页面和一些可能对内存很重的数据。但它用每个卷轴构建内存。我希望我的代码只添加一次子视图,然后滚动替换它并释放旧的(我使用ARC)。但内存正在积累,我认为这是因为它多次添加子视图。
- (void)scrollViewDidScroll:(UIScrollView *)sender
{
@synchronized(self) {
@autoreleasepool {
CGFloat pageWidth = mainScrollView.frame.size.width;
NSInteger page = round(mainScrollView.contentOffset.x / pageWidth);
int scrollOffset = (page * barWidth)-(mainScrollView.bounds.size.width/2);
if (scrollOffset<=0) {
scrollOffset = 0;
} else if (scrollOffset >= (pageIndicatorScrollView.contentSize.width - pageIndicatorScrollView.frame.size.width)) {
scrollOffset = (pageIndicatorScrollView.contentSize.width - pageIndicatorScrollView.frame.size.width);
}
[pageIndicatorScrollView setContentOffset:CGPointMake(scrollOffset, 0) animated:YES];
if(page < 0)
{
page = 0;
}
if(page >= [categoryScrollersArray count])
{
page = [categoryScrollersArray count] - 1;
}
//page = page +1;
//////////NSLog(@"page = %d",page);
// don't create or delete pages while rotating
if (!isRotating) {
if([categoryScrollersArray count] == 1)
{
if([categoryScrollersArray objectAtIndex:0] == [NSNull null])
{
[self loadScrollViewWithPage:0];
}
}
else if([categoryScrollersArray count] == 2)
{
if([categoryScrollersArray objectAtIndex:0] == [NSNull null])
{
[self loadScrollViewWithPage:0];
}
if([categoryScrollersArray objectAtIndex:1] == [NSNull null])
{
[self loadScrollViewWithPage:1];
}
}
else
{
if(page - 1 < 0)
{
if([categoryScrollersArray objectAtIndex:0] == [NSNull null])
{
[self loadScrollViewWithPage:0];
}
if([categoryScrollersArray objectAtIndex:1] == [NSNull null])
{
[self loadScrollViewWithPage:1];
}
}
else
{
if([categoryScrollersArray objectAtIndex:page - 1] == [NSNull null])
{
[self loadScrollViewWithPage:page - 1];
}
if([categoryScrollersArray objectAtIndex:page] == [NSNull null])
{
[self loadScrollViewWithPage:page];
}
if(page + 1 < [categoryScrollersArray count])
{
if([categoryScrollersArray objectAtIndex:page + 1] == [NSNull null])
{
[self loadScrollViewWithPage:page + 1];
}
}
}
if(page -2 >= 0 )
{
if([categoryScrollersArray objectAtIndex:page -2] != [NSNull null])
{
UIView *u = [categoryScrollersArray objectAtIndex:page-2];
//////////NSLog(@"remove page = %d",page-2);
[u removeFromSuperview];
if (page-2 < [categoryScrollersArray count]) {
if ([categoryScrollersArray objectAtIndex:page-2] == nil) {
[self showUnexpectedBehaviorAlertWithTest:@"An unknown error occured, please reopen the questionnaire. (1307)"];
}
else {
[categoryScrollersArray replaceObjectAtIndex:page-2 withObject:[NSNull null]];
}
}
else {
[self showUnexpectedBehaviorAlertWithTest:@"An unknown error occured, please reopen the questionnaire. (1314)"];
}
}
}
if(page +2 < [categoryScrollersArray count])
{
if([categoryScrollersArray objectAtIndex:page +2] != [NSNull null])
{
UIView *u = [categoryScrollersArray objectAtIndex:page+2];
// [u removeObserver:u.self forKeyPath:@"doReload"];
//////////NSLog(@"remove page = %d",page+2);
[u removeFromSuperview];
if (page+2 < [categoryScrollersArray count]) {
if ([categoryScrollersArray objectAtIndex:page+2] == nil) {
[self showUnexpectedBehaviorAlertWithTest:@"An unknown error occured, please reopen the questionnaire. (1329)"];
}
else {
[categoryScrollersArray replaceObjectAtIndex:page+2 withObject:[NSNull null]];
}
}
else {
[self showUnexpectedBehaviorAlertWithTest:@"An unknown error occured, please reopen the questionnaire. (1337)"];
}
}
}
}
}
[self changeProgressBar:page];
// ////////NSLog(@"viewControllers are %@",viewControllers);
// ////////NSLog(@"questionsScroll.subviews %@",questionsScroll.subviews);
}
}
}
另一个功能:
- (void)loadScrollViewWithPage:(int)page
{
if (page < 0)
return;
if (page >= [categoryScrollersArray count])
return;
//Categories * anCat;
//anCat = [catArray objectAtIndex:page];
// replace the placeholder if necessary
CategoryScrollerView *catScroll = [categoryScrollersArray objectAtIndex:page];
if ((NSNull *)catScroll == [NSNull null])
{
Step *aStep = [steps objectAtIndex:page];
NSString *coder = [[NSString alloc] init];
if ([self.visit.savedStatus integerValue] == 2 && [self.visit.state integerValue] == 1) {
coder = @"SAVED";
} else {
coder = @"OPEN";
}
NSString *questCode = [NSString stringWithFormat:@"%@",self.visit.questionnaire.version];
catScroll = [[CategoryScrollerView alloc] initWithFrame:mainScrollView.frame andStep:aStep andDelegate:self andStepname:aStep.name andVisitcode:coder andQuestionnairecode:questCode];
catScroll.autoresizesSubviews = YES;
catScroll.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin;
[self setNextBtnTitleForPage:page andView:catScroll];
if (page < [categoryScrollersArray count]) {
if ([categoryScrollersArray objectAtIndex:page] == nil) {
[self showUnexpectedBehaviorAlertWithTest:@"An unknown error occured, please reopen the questionnaire. (1101)"];
}
else {
[categoryScrollersArray replaceObjectAtIndex:page withObject:catScroll];
}
}
else {
[self showUnexpectedBehaviorAlertWithTest:@"An unknown error occured, please reopen the questionnaire. (1108)"];
}
}
// add the controller's view to the scroll view
if (catScroll.superview == nil)
{
CGRect frame = mainScrollView.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
catScroll.frame = frame;
[mainScrollView addSubview:catScroll];
// NSDictionary *numberItem = [self.contentList objectAtIndex:page];
// controller.numberImage.image = [UIImage imageNamed:[numberItem valueForKey:ImageKey]];
// controller.numberTitle.text = [numberItem valueForKey:NameKey];
}
}
答案 0 :(得分:0)
在一个视图控制器中执行此操作将非常复杂。特别是如果你也试图在那里做所有tableview数据源的东西。
使用UIPageViewController
会更好。
它是一个容器控制器(很像UINavigationController
或UITabBarController
),用于在分页滚动视图类型的视图中显示视图控制器。
这里有一些关于使用UIPageViewController
的教程。 http://www.raywenderlich.com/?s=uipageviewcontroller&cof=FORID%3A10
基本上,您为每个页面传递一个不同的表视图控制器。它需要一些设置,但一旦完成它将有助于使它更容易,并将减少内存使用。
还有更多教程...... http://www.appcoda.com/uipageviewcontroller-tutorial-intro/
此外,您不必全屏显示。您只能将页面视图控制器用于要滚动的部分屏幕。