How to dynamically set the height of a UITableView?

后端 未结 5 2174
独厮守ぢ
独厮守ぢ 2020-12-29 14:23

I have a tableview with different cell heights using the heightForRowAtIndexPath: method.

I would like to dynamically set the height of a UITable

5条回答
  •  灰色年华
    2020-12-29 14:49

    Add an observer for the contentSize property on the table view, and adjust the frame accordingly

    [self.tableView addObserver:self forKeyPath:@"contentSize" options:0 context:NULL];
    

    then in the callback:

    - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
    {
        CGRect frame = self.tableView.frame;
        frame.size = self.tableView.contentSize;
        self.tableView.frame = frame;
    }
    

提交回复
热议问题