I have created a UITableview
with custom UITableViewCell
.The UITableViewCell
contains UILabels
and I have calculated the heig
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);
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];
}
}
on iOS 8+ this worked to get the table view Height
CGFloat tableViewHeight = tableView.bounds.size.height;
Very simplest way to get your UITableView height
- (CGFloat)tableViewHeight
{
[tblData layoutIfNeeded];
return [YOUR_TABLE_NAME contentSize].height;
}
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
propertyQED