【发布时间】:2014-01-04 01:08:35
【问题描述】:
我对表格视图及其响应有疑问,因为每个单元格都有带有 72 个标签的滚动视图。我知道 scrollView 需要首先加载所有元素,然后加载到屏幕上,并且由于该表视图很慢,但是有没有办法在每次创建标签时不分配和调用 initWithFrame 方法?我尝试重复使用不同框架的标签,但它不起作用。
这是我需要以某种方式优化以更快地创建标签的代码。
int listSize = 36;
for(int i=0;i<listSize;i++){
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0+i*200, 0, 200, 80)];
label.text = @"HELLO";
label.textColor = [UIColor blackColor];
label.backgroundColor = [UIColor clearColor];
label.textAlignment = NSTextAlignmentCenter;
label.font = [UIFont fontWithName:@"ArialMT" size:18];
[scrolView addSubview:label];
UILabel *grayBorderInFront = [[UILabel alloc] initWithFrame:CGRectMake(2+i*200, 5, 1, scrolView.frame.size.height-10)];
grayBorderInFront.text = @"";
grayBorderInFront.backgroundColor = [UIColor lightGrayColor];
[scrolView addSubview:grayBorderInFront];
}
【问题讨论】:
-
你想达到什么目标?也许如果您向我们展示一些代码,我们可以帮助您。
-
我希望我的数据可以在滚动视图(左右)中滚动,但我也想通过列表视图向下滚动。但是现在我上下或上升都太慢了。
-
您应该继承 UITableViewCell 的子类,而不是为每个单元格创建新标签,并在表格滚动时重置文本。您可以覆盖
prepareForReuse将单元格重置为默认状态,然后只需设置该行的标签文本。 -
仅将可见标签放入视图中。然后实现委托函数
scrollviewdidscroll,并在它们需要变得可见时创建新标签。
标签: ios objective-c uitableview memory uiscrollview