Reputation: 12534
I am trying to create a non-scrollable tableview that adjusts its size based on its content.
However, I am able to do one of two things. Either:
Any idea of how I can go about accomplishing this?
Upvotes: 1
Views: 41
Reputation: 10434
You can observe the changes in the contentsize as this.
- (void)viewDidLoad {
[super viewDidLoad];
[self.tableView addObserver:self forKeyPath:@"contentSize" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context
{
if ([keyPath isEqualToString:@"contentSize"]) {
CGSize newSize = [[change objectForKey:NSKeyValueChangeNewKey] CGSizeValue];
CGSize oldSize = [[change objectForKey:NSKeyValueChangeOldKey] CGSizeValue];
if (newSize.height != oldSize.height) {
// use newSize.height for your process
}
}
}
make sure observer is removed
- (void)dealloc {
if(self.isViewLoaded) {
[self.tableView removeObserver:self forKeyPath:@"contentSize"];
}
}
Upvotes: 1