Mani
Mani

Reputation: 1310

Xcode Scrollview slow

I have create a labels with in the scroll view. The for loop count is more than 1000. At the time the scroll is very slow. If the data is less amount (200, 300, ...) at the time its scroll smoothly. I am using the below code for create label.

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

How can I fix this issue?

Thanks in advance.

Upvotes: 0

Views: 832

Answers (2)

Or Ron
Or Ron

Reputation: 2343

This happens to you because you load to many things to the memory

As the comment above said, It will be very easy to achieve using UITableView.

Although if you want more control, and you decide using scroll view you have to implement lazy loding. This is being done be allocating and placing the labels when it actually needed to be showen and not during the initialization. You will be able to know that by setting you view controller as the delegate and get the contentOffset from the scrollViewDidScroll: method. Also after recognizing you'll need te remove the subview in order to clear the memory

You can see good example for it here

Upvotes: 2

BooRanger
BooRanger

Reputation: 1526

I feel its because ios is trying to create and keep alot of these views in memory rather then create them dynamically(am I using that word right?) as they scroll on. It would be alot faster to use a UITableView that has a reuse identifier. This will stop iOs from creating a new allocation of memory for each view you make. Create a custom UTTableViewCell, with the labels and buttons you need that is created via a reuse identifier.

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

You should be able to find alot of resource on the web for creating a UITableView with a custom style. If you need a horizontal scrolling UITableView take a look at EasyTableView.

Upvotes: 0

Related Questions