Get UITableView's height

前端 未结 5 1394
终归单人心
终归单人心 2021-02-20 10:27

I have created a UITableview with custom UITableViewCell.The UITableViewCell contains UILabels and I have calculated the heig

相关标签:
5条回答
  • 2021-02-20 10:50

    Use contentSize.height property of UITableView.

    I think you want to set the whole tableview with content size and then set the scrollview size related content of UITableView and for this use bellow code...

    After add data or reloadData in UITableView just set bellow code..

        yourTableView.frame = CGRectMake(yourTableView.frame.origin.x,yourTableView.frame.origin.y, yourTableView.frame.size.width, yourTableView.contentSize.height);
    
        float ftbl = yourTableView.frame.origin.y + yourTableView.contentSize.height + 15;
        yourScrollView.contentSize=CGSizeMake(320, ftbl);
    

    UPDATE:

    self.tableView=[[UITableView alloc] initWithFrame:CGRectMake(0,150,327,[arr5 count]*205)style:UITableViewStylePlain]; 
    self.tableView.delegate=self; 
    self.tableView.dataSource=self;
    
    [testscroll addSubview:self.tableView]; /// add this tableview in scrollview not in self.view
    [self.tableView reloadData];
    self.tableView.frame = CGRectMake(self.tableView.frame.origin.x, self.tableView.frame.origin.y, self.tableView.frame.size.width, self.tableView.contentSize.height);
    float ftbl = self.tableView.frame.origin.y + self.tableView.contentSize.height + 15; 
    testscroll.contentSize=CGSizeMake(320, ftbl); 
    
    0 讨论(0)
  • 2021-02-20 10:51

    You also can use KVO to observer tableview's contentSize property and adjust what you need in other views.

    Put it somewhere, e.g. in viewDidLoad:

    [self.tableView addObserver:self forKeyPath:@"contentSize" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionInitial context:nil];
    

    Then implement observer:

    - (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
          if (object == self.tableView) {
            self.scrollViewHeightConstraint.constant = self.tableView.contentSize.height;
          }
          else {
            [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
          }
        }
    
    0 讨论(0)
  • 2021-02-20 10:53

    on iOS 8+ this worked to get the table view Height

    CGFloat tableViewHeight = tableView.bounds.size.height;
    
    0 讨论(0)
  • 2021-02-20 11:02

    Very simplest way to get your UITableView height

    - (CGFloat)tableViewHeight
    
    {
       [tblData layoutIfNeeded];
    
       return [YOUR_TABLE_NAME contentSize].height;
    }
    
    0 讨论(0)
  • 2021-02-20 11:08

    Try this: tableView.backgroundView.bounds.size.height Should work

    Logic:

    • UITableView has a property, backgroundView which is "A table view’s background view is automatically resized to match the size of the table view."
    • backgroundView is a UIView which has the property bounds which is a CGRect that "defines the size and position of the view."
    • CGRect has a size property, size has a height property

    QED

    0 讨论(0)
提交回复
热议问题