James Harpe
James Harpe

Reputation: 4345

iOS: Can't scroll between UITableViews in paged UIScrollView

I have a UIScrollView, in which I want to contain a number of UITableViews, each one being a page. I've added them to my scroll view, and verified that the frames of the UITableViews are set correctly. The first UITableView is displayed correctly, but it will not let me scroll horizontally to see the others. Here's the code I'm using to at the table views:

scrollView.delegate=self;
scrollView.scrollEnabled=YES;
tableViews = [[NSMutableArray alloc] initWithCapacity:recipOrgs.count];
for (NSDictionary *orgDict in recipOrgs) {

    int index = [recipOrgs indexOfObject:orgDict];


    NSLog(@"Creating table view for index: %d",index);
    CGRect frame;
    frame.origin.x = self.scrollView.frame.size.width * index;
    frame.origin.y = 0;
    frame.size = self.scrollView.frame.size;

    OrganizationTableView *tableView = [[OrganizationTableView alloc] init];

    tableView.index=index;
    tableView.parentCon=self;
    tableView.dataSource=tableView;
    tableView.delegate=tableView;

    [tableViews addObject:tableView];
    [scrollView addSubview:tableView];
    [tableView setFrame:frame];

}

Any ideas why this isn't working? As I mentioned, I checked the x values of the UITableView origins, and I get 0, 320, 640, etc. like I would expect.

Upvotes: 0

Views: 119

Answers (1)

dariaa
dariaa

Reputation: 6385

You are probably forgetting to set the contentSize for your scrollView.

scollView.contentSize = CGSizeMake(recipOrgs * CGRectGetWidth(scrollView.frame), CGRectGetHeight(scrollView.frame));

But you should probably consider using UICollectionView instead of UIScrollView, then you get reuse logic for free and you do not have to worry about the contentSize.

Upvotes: 1

Related Questions