【问题标题】:Multiple Views in ScrollViewScrollView 中的多个视图
【发布时间】:2012-10-13 19:52:43
【问题描述】:

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

 -(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

【问题讨论】:

  • 这就是他们所说的匈牙利符号吗?

标签: iphone ios memory-management uiview uiscrollview


【解决方案1】:

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

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

因此,如果您的滚动视图中有一个大的调查视图,并且许多问题视图在调查视图中都有多个视图,那么您可以例如消除调查视图并仅在滚动视图中布置可见的问题视图的内容视图。

【讨论】:

  • 问题是当我将视图添加到滚动视图时,应用程序运行速度太慢。我想它填满了记忆。 @Caleb
  • 对——这就是为什么您要将视图限制为仅可见的视图。当用户滚动时,您将删除一些并添加其他的,具体取决于滚动的方向。
  • 你知道我该怎么做吗?滚动删除视图并添加其他视图。有代表什么的吗?
【解决方案2】:

由于我们正在讨论内存管理,因此我想在这里更正一些问题,以实现最佳内存管理实践。

你不应该使用,

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];

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-09
    • 1970-01-01
    • 2015-08-22
    相关资源
    最近更新 更多