UIScrollView填充速度很慢

时间:2015-03-24 19:41:34

标签: ios objective-c performance uiscrollview uicollectionview

我在iOS7 +应用中有这个分页UIScrollView,每个页面都包含一个UIView,其中包含一个UITextView

这是填充它的代码。

// Looping through results
for(int i = 0; i < [data count]; i++) {
    // Creating view
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(i * scrollSize.width, 0, scrollSize.width, scrollSize.height)];

    // Creating the UITextView
    UITextView *theText = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, scrollSize.width * 0.9, scrollSize.height * 0.5)];
    theText.backgroundColor = nil;
    theText.scrollEnabled = NO;
    theText.selectable    = NO;
    theText.center = CGPointMake(view.frame.size.width / 2, view.frame.size.height / 2);

    // Assigning properties
    theText.attributedText = [[NSAttributedString alloc] initWithString:[data objectAtIndex:i] attributes:attribute];

    // Centering the label and assigning font size
    theText.font = [UIFont fontWithName:FONT_NAME size:fontSize];
    theText.textColor = FONT_COLOR;

    // Adding label to view and view to scrollView
    [view addSubview:theText];
    [scroll addSubview:view];
}

它很慢,阻止了应用的用户界面。 (对于30个元素的data,大约0.38秒。)

我试图通过不每次创建子视图来优化它,但看起来我无法复制原始对象。

如何让它更快?我可以在循环中重用一些元素吗?我应该使用像UICollectionView这样的用户吗? UICollectionView是否符合我的需求?

谢谢!

0 个答案:

没有答案