3

Currently, I've developed project that UIScrollView, UITableView, another 3 more UIView inputs and UIButton at the last. In that page, UIScrollView height will be dynamically increased based on height of UITableView.

For UITableView there is no more scrolling. Its height will be increased as well based on how many rows are added based on JSON data loaded by Async as follow.

productHeight = 44;
productHeight *= _nsOrderItems.count;
productHeight = productHeight + 100;

if (isHeaderTap) {
    self.productTableHeight.constant = 50;
} else {
    self.productTableHeight.constant = productHeight;
}

//then the tableView must be redrawn
[self.productTable setNeedsDisplay];

My Problem is I want to increase height of UIScrollView based on height of UITableView.

- (void)viewDidLayoutSubviews {
    [self.scrollView setContentSize:CGSizeMake(_scrollView.frame.size.width, _btnEdit.frame.origin.y + _btnEdit.frame.size.height)];
}
PPShein
  • 13,309
  • 42
  • 142
  • 227
  • This link help for you http://stackoverflow.com/questions/35787737/how-to-create-dynamic-tableview-cell-with-dynamic-tableview-height-in-ios/35788473#35788473 – Shrikant Tanwade Mar 09 '16 at 05:29
  • i think your table view height is not increaing,,, check your bottom constraints of uiscrollview – Muhammad Raheel Mateen Mar 09 '16 at 05:34
  • 1. i suggest if you can Manage buttons or other 3 views in tableview then its well and good. 2. Implement UIScrollViewDelegate in your "Viewcontroller" , where you can set content size "- (void)scrollViewDidScroll:(UIScrollView *)scrollView" method. – Wolverine Mar 09 '16 at 05:50
  • @ShrikantTanwade can u post as answer? – PPShein Mar 09 '16 at 07:49

2 Answers2

6

You can definitely do that,

  1. First make sure your constraint of cells subView must set to top to bottom in order to calculate the height required for the cell.

  2. Make sure your delegated are set as below

    -(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
      return 44;
     } 
    
     -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewAutomaticDimension;
     }
    
  3. Set height constraint of your tableView and make outlet of that constraint.

  4. Add below method to your class where you want to resize your tableView dynamically.

    - (void)adjustHeightOfTableview
    {
        CGFloat height = self.tableView.contentSize.height;
        //CGFloat maxHeight = self.tableView.superview.frame.size.height - self.tableView.frame.origin.y;
    
       /* 
        Here you have to take care of two things, if there is only    tableView on the screen then you have to see is your tableView going below screen using maxHeight and your screen height,
     Or you can add your tableView inside scrollView so that your tableView can increase its height as much it requires based on the number of cell (with different height based on content) it has to display.
       */
    
       // now set the height constraint accordingly
        self.constraintHeightTableView.constant = height;
    
       //If you want to increase tableView height with animation you can do that as below.
    
        [UIView animateWithDuration:0.5 animations:^{
        [self.view layoutIfNeeded];
        }];
    }
    
  5. Call this method when you are ready with the dataSource for the table, and call the method as

    dispatch_async(dispatch_get_main_queue(), ^{
    
       [self.tableView reloadData];
    
       //In my case i had to call this method after some delay, because (i think) it will allow tableView to reload completely and then calculate the height required for itself. (This might be a workaround, but it worked for me)
       [self performSelector:@selector(adjustHeightOfTableview) withObject:nil afterDelay:0.3];
    });
    
Shrikant Tanwade
  • 1,391
  • 12
  • 21
1
// Get the supposed to be height of tableview 
CGFloat height = self.tableViewMenu.contentSize.height;
 // get the height constraint of tableview and give it you respected height
self.constraintHeightTableView.constant = height;
    // set the scroll 

NOTE:- don't forget to disenable the scrolling of table from storyboard or programmatically

self.scrollView.contentSize = CGSizeMake(self.scrollView.contentSize.width, self.viewUserLogin.frame.size.height + height);
    [self.scrollView layoutIfNeeded];
Vaibhav Gaikwad
  • 396
  • 5
  • 13