I want to create suggestions to UISearchBar so I added UITableView and I want to change height of the UITableView by content.
When I get data I call:
[self.searchTableView reloadData];
and in - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
I am trying to resize tableView:
CGRect bounds = [tableView bounds];
NSLog(@"%f",tableView.contentSize.height);
[tableView setBounds:CGRectMake(bounds.origin.x,
bounds.origin.y,
bounds.size.width,
20 * [self.suggestionData count])];
/*
CGRect frame = self.searchTableView.frame;;
frame.size.height = 20 * [self.suggestionData count];
self.searchTableView.frame = frame;
*/
(20 is height of 1 row.) I've tried tableView.contentSize.height too but it didn 't works. Maybe there is need to change something in Storyboard or I when I was trying to find solution I change something wrong. I just get height of tableview same as I set in storyboard. Thanks
Since UITableView
extends a UIScrollView
I suggest you to use the following approach in order to get a table view whose bounds fit its content size.
In the -init
or -awakeFromNib
of your special TableView (you can extend the UITableView or create a category) register the observer
[self addObserver:self forKeyPath:@"contentSize" options:NSKeyValueObservingOptionNew context:nil];
and then add
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if (object == self && [keyPath isEqualToString:@"contentSize"]) {
CGRect frame = self.frame;
CGFloat currentHeight = self.contentSize.height;
if (fabs(currentHeight - frame.size.height) > FLT_EPSILON) {
frame.size.height = self.contentSize.height;
self.frame = frame;
}
} else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:nil];
}
}
Remember to deregister the observer in the -dealloc
- (void)dealloc {
[self removeObserver:self forKeyPath:@"contentSize"];
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments