Frage

am trying to place a ScrollView in my app that has 1000,000 record, this scrollView will load when the app launches, so the app is not running until the million 1000 000 record which takes a lot of time, i was wondering is there any way to show the app and the scrollView while records are loading (show the scrollView while adding its records), below the code am using:

    - (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    [self loadIt];
}

- (void)loadIt{

    float startX = 0;
    float startY = 0;
    [_bigScroll setContentSize:CGSizeMake(320, 312500)];
    _bigScroll.pagingEnabled = NO;
    for (counter=0; counter<999999; counter++)
    {
        UIButton *tester=[[UIButton alloc] initWithFrame:CGRectMake(startX, startY, 10, 10)];

        if (counter % 2 == 0) {

            [tester setBackgroundColor:[UIColor whiteColor]];

        }
        else
        {

            [tester setBackgroundColor:[UIColor grayColor]];
        }

        [_bigScroll addSubview:tester];
        [tester release];

        if (startX == 320) {
            startX = 0;
            startY += 10;
        }
        else
            startX += 10;

        NSLog(@"counter  =  %d", counter);
    }

}

Please advice.

War es hilfreich?

Lösung

Is there any way to show the app and the scrollView while records are loading ?

Try to use [self performSelector:@selector(loadIt) withObject:nil]; or [self performSelector:@selector(loadIt) withObject:nil afterDelay:0.2];
It will not block your UI until the execution of this method.

You are loading lots of records. Actually you should not load all records at at time. You should use mechanism something like tableview is using i.e.load only those record which are in visible area of scrollview. Don't load new rows until the scroll and you should reuse row or views so speedup the scrolling.

Andere Tipps

Apple's documentation for UIScrollView is very clear that the scrolled view should be tiled, with your application providing tiles as the view scrolls.

The object that manages the drawing of content displayed in a scroll view should tile the content’s subviews so that no view exceeds the size of the screen. As users scroll in the scroll view, this object should add and remove subviews as necessary.

This is necessary both for performance and memory usage: the scrollable view is backed by a CALayer, which in turn is backed by a bitmap. The same is true for each of the UIButton objects created.

Whilst it is not surprising that this takes a long time, it's more of a mystery that your app hasn't been terminated for using too much memory.

Both UITableView and UICollectionView are examples of views that tile their content. You may find you can use one of these to implement you requirements, and if not, follow the model they use.

You don't need to create 1000,000 views . You can create views dynamically and remove the previous views those are not visible at the screen space. So at the time of scrolling you can create new views and remove the views those are out of visible area of screen. This will help you to save memory otherwise whether you are using ARC in your project if you load that much number of views in memory there will surely a chance of crash , ARC will not help you in that case.

once try this Change the code in the

-viewdidload()
{
    [self loadIt];//change this to 
    [self performSelectorInBackground:@selector(loadIt) withObject:nil];
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top