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.
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 memoryYou can see good example for it here