ScrollView中的多个视图

时间:2012-10-23 19:40:04

标签: iphone ios memory-management uiview uiscrollview

我正在尝试制作一个调查应用。一个滚动视图中有20个视图和许多uitextviews。当我尝试这样做时,应用程序的工作速度非常慢。我想我不能正确地进行内存管理。你能帮我做对吗?非常感谢你。这是我的代码:

 -(void)textViewBicimle: (UITextView *)textAlani{
[textAlani.layer setBorderColor:[[[UIColor blackColor] colorWithAlphaComponent:1.0] CGColor]];
[textAlani.layer setBorderWidth:1.0];
  }

  - (void) tableCiz:(NSMutableArray *)hucreler sutun:(int)sutunSayisi satir:     (int)satirSayisi{
 int z = 0;
 for (int i =0;i<satirSayisi;i++){
     for(int j=0;j<sutunSayisi&&z<satirSayisi*sutunSayisi;j++,z++){
         [self textViewBicimle:[hucreler objectAtIndex:z]];
         [[hucreler objectAtIndex:z] setFrame:CGRectMake((20+j*100), (20+i*40), 100, 40)];
     }
 }
 }

 -(void)hucreArrayOlustur: (int)sutunSayisi suankiView:(UIView *)soruViewIki satir:(int)satirSayisi{
for (int i = 0; i <sutunSayisi*satirSayisi; i++){
    self.textView = [[UITextView alloc]init];
    self.textView.text = @"dsgfdh";
    [self.hucreArray addObject:self.textView];
    [soruViewIki addSubview:self.textView];
    [self.textView release];
}
}

-(void)viewOlustur{
 for (int i = 0; i <soruAdedi; i++){
self.hucreArray = [[[NSMutableArray alloc]init]autorelease];
if (i == 0){
    self.soruView = [[UIView alloc]initWithFrame:CGRectMake(20, 20, 728, 0)];
    self.soruView.clipsToBounds = YES;
    self.soruView.backgroundColor = [UIColor lightGrayColor];
    [self hucreArrayOlustur:5 suankiView:self.soruView satir:10];
    [self tableCiz:self.hucreArray sutun:5 satir:10];
    CGRect frame = self.soruView.frame;
    CGRect frame2 = [[self.hucreArray objectAtIndex:49] frame];
    frame.size.height =  frame2.origin.y+frame2.size.height+34;
    self.soruView.frame = frame;
    [self.viewArray addObject:self.soruView];
    [scrollView addSubview:self.soruView];
    [self.soruView release];
}
else{
    CGRect frameGecici = [[self.viewArray objectAtIndex:i-1] frame];
    float geciciY = frameGecici.origin.y+frameGecici.size.height+1;
    self.soruView = [[UIView alloc]initWithFrame:CGRectMake(20, geciciY, 728, 0)];
    self.soruView.clipsToBounds = YES;
    self.soruView.backgroundColor = [UIColor lightGrayColor];
    [self hucreArrayOlustur:5 suankiView:self.soruView satir:10];
    [self tableCiz:self.hucreArray sutun:5 satir:10];
    CGRect frame = self.soruView.frame;
    CGRect frame2 = [[self.hucreArray objectAtIndex:49] frame];
    frame.size.height =  frame2.origin.y+frame2.size.height+34;
    self.soruView.frame = frame;
    [self.viewArray addObject:self.soruView];
    [scrollView addSubview:self.soruView];
    [self.soruView release];
}

}
}

- (void)viewDidLoad
{
[super viewDidLoad];
scrollView.contentSize = CGSizeMake(0, 10000);
self.viewArray = [[NSMutableArray alloc]init];
[self viewOlustur];
NSLog(@"%@",self.viewArray);
// Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
 [super didReceiveMemoryWarning];

 // Dispose of any resources that can be recreated.
}

 - (void)dealloc {
 [self.soruView release];
 [self.textView release];
 [scrollView release];
 [super dealloc];
 }

 - (void)viewDidUnload {
 [self setScrollView:nil];
 [super viewDidUnload];
  }
  @end

2 个答案:

答案 0 :(得分:0)

UIScrollView reference page的以下建议可能有所帮助:

  

管理滚动中显示的内容绘制的对象   视图应该平铺内容的子视图,以便没有视图超过   屏幕大小。当用户在滚动视图中滚动时,此对象   应根据需要添加和删除子视图。

因此,如果您在滚动视图中有一个大的调查视图,并且许多问题视图在调查视图中都有多个视图,您可以例如删除调查视图并在滚动中仅列出可见问题查看内容视图。

答案 1 :(得分:0)

由于我们正在谈论内存管理,我想在这里纠正一些事情,只是为了最好的内存管理实践。

你不应该使用,

self.textView = [[UITextView alloc]init];

而是将其用作,

self.textView = [[[UITextView alloc] init] autorelease];

不要直接在属性上使用[self.textView release];。如果您不使用ARC,可以将以下行放在dealloc方法中,

self.textView = nil;

如果您可以将某个变量作为局部变量,则不需要将其用作属性。

而不是,

self.soruView = [[UIView alloc]initWithFrame:CGRectMake(20, 20, 728, 0)];

可能你可以用它作为,

UIView *soruView = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 728, 0)];
//rest of the code here
[self.scrollView addSubview:soruView];
[soruview release];

如果您不需要同时显示所有视图,则可以在用户滚动到该点时添加其余视图。这也有助于优化内存使用。