views:

86

answers:

2

I have a bunch of 'rowviews' that I want to put in a vertical scroll view. I have created this rowView view as a separate nib in IB. They are sized at 1024/200 (ipad). Now I want to put them one by one in my parent UIScrollView. I tried a simple [vScroll addSubview:rowView] but this puts them overtop of eachother (I made the rowview transparent to check this). So then I started fooling with the bounds of each rowview to no avail. This is my code. Note 'self.yExtentSoFar' is initialised to 0. Imagine the code below called for each row:

            MyRowView *rowView = [[MyRowView alloc] init];

    float calculatedWidth = 0;
    // minus nav bar
    float calculatedHeight = 0;
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    UIInterfaceOrientation orientation = [UIDevice currentDevice].orientation;
    if (orientation == UIInterfaceOrientationPortrait){
        // iPad
        calculatedWidth =  768.0;
        calculatedHeight = 960.0;
    }else{
        // iPad
        calculatedWidth =  1024.0;
        calculatedHeight = 704.0;
    }

    [self.vScroll addSubview: rowView.view];
    [rowView.view setBounds:CGRectMake(0, self.yExtentSoFar, calculatedWidth,200)];
    [self.vScroll setContentSize:CGSizeMake(calculatedWidth, yExtentSoFar+200)];
    self.yExtentSoFar += 200;

So before I tried settings bounds the rowviews appeared overtop of each other. Understandable I guess. When I set the bounds, the 2nd row view hasn't appeared under the 1st as expected, instead I have to pull down the vScroll and the 2nd has appeared ABOVE the first off screen!

Could someone point to where I'm going wrong? Thanks a lot,

Mike

+1  A: 

You're doing it wrong ;-) What you have explained here is more or less a re-implementation of what you get using a UITableView. Use a UITableView and a custom table view cell. It will make your life much easier.

Matt Long
Using a custom UITableCell view sounds more complex. I just want to chuck my views in a UIScrollView. Why are they appearing on top of each other (off the screen) instead of below each other. I can't imagine whenever a vertical scroll situation is needed people use a UITable.
Mike Simmons
Not to be snarky, but actually, whether you can imagine it or not, people do use UITableViews very often. According to your description in your question it's exactly what you need. Learn how to use table views. As I said before, it will make your life *much* easier. Trust me. ;-)
Matt Long
First of all thankyou for your answer. I just feel that re-using an existing component is only useful if you are getting functionality for free you would otherwise have to create. Once I implement all the delegate methods and stuff for tables and fool around with custom table cell views I could have written the 3 lines for a vScrollView. I also just wanted to know out of interest what was going wrong with my thing. @ohhorob answered that for me. Thankyou for your answer though! I'll give them a try next time.
Mike Simmons
+1  A: 

You want to layout your subviews by setting their frame.

Specifically you're confusing the reference co-ordinates.. bounds refers to how much of that view to show. Whereas the frame is where (& what size) should the view be placed in it's superview.

See "The Relationship of the Frame, Bounds, and Center" View Programming Guide for iPhone

ohhorob
Perfect!!! Thankyou :)
Mike Simmons
Also, when working with a collection of views in a `UIScrollView`, I keep them in a container view. This helps when I need to iterate over the "scrolled" subviews to do work with them. If you rely on `myScrollView.subviews` you will get the scroll bar indicator views included! (the container view is my only subview of `myScrollView`)
ohhorob