Andrey
Andrey

Reputation: 2729

UIScrollView: addSubview does not add item

I spent a few hours, trying to solve my problem, but I can't find solution myself. I have some code, adding UIView to my scrollView:

- (void) loadSpareparts:(VitoParts*) parts{
    data = [VitoServer fetchPartList:parts];

    int itemHeight = 100;
    [scrollView setContentSize:CGSizeMake(scrollView.frame.size.width, (data.count * itemHeight))];

    NSLog(@"Item count: %d", data.count);

    for (int i=0; i<data.count; i++) {
        VitoPart *sparepart = [data objectAtIndex:i];
        VIOnePart *partView = [[VIOnePart alloc] init];

        [partView setFrame:CGRectMake (0, (i * itemHeight), scrollView.frame.size.width, itemHeight)];

        [partView setData:sparepart];
        [scrollView addSubview:partView];
        //        [self.view addSubview:partView];
    }

    NSLog(@"In scrollView: %d", scrollView.subviews.count);
}

But it is not working. My last log message tells, what 0 items in scrollView. If I add partView to self.view, it is working OK. IBOutlet also is connected to ScrollView in XIB-editor.

Where I should try to find solution?

Thanks.

Upvotes: 0

Views: 2122

Answers (1)

Alex
Alex

Reputation: 8991

Are you calling this method before or after -(void)viewDidLoad? If before, the view will not have loaded hence scrollView will be nil.

Upvotes: 2

Related Questions