0

我在滚动视图中创建了一个标签。for循环计数超过1000。当时滚动很慢。如果数据量较少(200、300,...),则其滚动顺畅。我正在使用以下代码创建标签。

UILabel     *modelLabel;
UIButton    *modelButton;

for (int i = 0; i < [modelArray count]; i++)
{
    modelButton                 = [UIButton buttonWithType:UIButtonTypeCustom];
    modelButton.frame           = CGRectMake(0.0, (i * LABEL_HEIGHT), LABEL_WIDTH, LABEL_HEIGHT);
    modelButton.tag             = i+100;
    [modelButton addTarget:nil action:@selector(modelButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [modelScrollView addSubview:modelButton];
    modelLabel                  = [[UILabel alloc] initWithFrame:CGRectMake(0.0, (i * LABEL_HEIGHT), LABEL_WIDTH, LABEL_HEIGHT)];
    modelLabel.text             = [NSString stringWithFormat:@"%@", [[modelArray objectAtIndex:i] valueForKey:@"Model"]];
    modelLabel.tag              = i+1000;
    modelLabel.backgroundColor  = [UIColor clearColor];
    modelLabel.textColor     = [UIColor grayColor];
    modelLabel.alpha         = 0.5;
    modelLabel.textAlignment    = UITextAlignmentCenter;
    modelLabel.font             = EUROSLITE_FONT(14);
    [modelScrollView addSubview:modelLabel];
}

[modelScrollView setContentSize:CGSizeMake(280.0, ([modelArray count] * LABEL_HEIGHT))];

我该如何解决这个问题?

提前致谢。

4

2 回答 2

2

这发生在您身上,因为您将许多东西加载到内存中

正如上面的评论所说,使用UITableView.

虽然如果你想要更多的控制,并且你决定使用滚动视图,你必须实现延迟加载。这样做是在实际需要显示标签时而不是在初始化期间分配和放置标签。scrollViewDidScroll:通过将视图控制器设置为委托并从方法中获取 contentOffset,您将能够知道这一点。同样在识别出您需要删除子视图以清除内存后

你可以在这里看到很好的例子

于 2013-01-28T14:45:24.783 回答
0

我觉得这是因为 ios 试图在内存中创建并保留很多这些视图,而不是在它们滚动时动态创建它们(我使用那个词对吗?)。使用具有重用标识符的 UITableView 会快得多。这将阻止 iO 为您创建的每个视图创建新的内存分配。创建一个自定义 UTTableViewCell,其中包含通过重用标识符创建的所需标签和按钮。

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 

您应该能够在网络上找到大量资源来创建具有自定义样式的 UITableView。如果您需要水平滚动 UITableView 看看EasyTableView

于 2013-01-28T14:44:57.957 回答