Erik
Erik

Reputation: 1254

uitableview autoscrolling when I programatically set the height

I have a set of UITableViewControllers which I add to a container and superview using this protocol method in the supercontroller:

-(void)indexPathSelected:(NSIndexPath*)selectedIndex;
{
//This method is fired when the index path is selected in the content container. The content container
//could contain WobletTableViewController, WobletDetailViewController or the EventsTableViewController
for (UIViewController *childViewController in [self childViewControllers])
{
    if ([childViewController isKindOfClass:[WobletTableViewController class]])
    {
        UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        TableDetailViewController *rewardsTableVC = [sb instantiateViewControllerWithIdentifier:@"restaurantDetailsTable"];
        //tvc.delegate=self;
        //found container view controller
        TableDetailViewController *restaurantsTableVC = (TableDetailViewController *)childViewController;
        [restaurantsTableVC removeFromParentViewController];
        [restaurantsTableVC.view removeFromSuperview];

        rewardsTableVC.rewards=[(NSDictionary*)[self.restaurants objectAtIndex:selectedIndex.row] objectForKey:@"rewards"];
        [self addChildViewController:rewardsTableVC];
        [self.contentContainer addSubview:rewardsTableVC.view];
    }
}

}

This works fine, as far as I am aware. Next, I had to size the cells so I implemented the delegate method for this as such:

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:    (NSIndexPath*)indexPath
{
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            return 347;
        }
    }
    return 71;
}

The cells are created pretty standardly, as well:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:  (NSIndexPath *)indexPath
{
UITableViewCell *cell;
if(indexPath.row==0)
{
    NSString *CellIdentifier = @"topDetailCell";
    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    assert([cell isMemberOfClass:[HeaderCell class]]);
}
else
{
    NSString *CellIdentifier = @"rewardCell";
    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    assert([cell isMemberOfClass:[RewardCell class]]);
}
return cell;
}

After adding this method, the embedded tableview automatically scrolls to the top after you scroll. This behavior was not happening before adjusting the height. What's up with this?

Upvotes: 1

Views: 93

Answers (1)

Erik
Erik

Reputation: 1254

There was a simple problem here when I was programmatically creating the table view. Since it was sitting inside a container view, it needed to have it's frame set to the container view's bounds:

-(void)indexPathSelected:(NSIndexPath*)selectedIndex;
{
//This method is fired when the index path is selected in the content container. The content container
//could contain WobletTableViewController, WobletDetailViewController or the EventsTableViewController
for (UIViewController *childViewController in [self childViewControllers])
{
    if ([childViewController isKindOfClass:[WobletTableViewController class]])
    {
        UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        TableDetailViewController *rewardsTableVC = [sb instantiateViewControllerWithIdentifier:@"restaurantDetailsTable"];
        //tvc.delegate=self;
        //found container view controller
        TableDetailViewController *restaurantsTableVC = (TableDetailViewController *)childViewController;
        rewardsTableVC.view.frame=self.contentContainer.bounds;
        [restaurantsTableVC removeFromParentViewController];
        [restaurantsTableVC.view removeFromSuperview];

        rewardsTableVC.rewards=[(NSDictionary*)[self.restaurants objectAtIndex:selectedIndex.row] objectForKey:@"rewards"];
        [self addChildViewController:rewardsTableVC];
        [self.contentContainer addSubview:rewardsTableVC.view];
    }
}
}

Upvotes: 1

Related Questions