Get UITableView's height

前端 未结 5 1411
终归单人心
终归单人心 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: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 *)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];
          }
        }
    

提交回复
热议问题