Reputation: 2880
I need to make UITaableView
to fit its content.I m loading array data in UITableView
.But when I fixed some static size ,eventhough the items are less it is showing the extra space.So I need to make dynamic height to UITableView
but could not achieve it ..
autocompleteTableView = [[UITableView alloc] initWithFrame:CGRectMake(10, 140, 200, self.autocompleteTableView.contentSize.height) style:UITableViewStyleGrouped];
autocompleteTableView.delegate = self;
autocompleteTableView.dataSource = self;
autocompleteTableView.scrollEnabled = YES;
[self.view addSubview:autocompleteTableView];
When I write this I could not see the TableView itself..How can I get it ?
Upvotes: 1
Views: 1647
Reputation: 31081
First you can get your content(text) height and then according to this height you can adjust your tableView frame.
CGSize size = [@"Your string"
sizeWithFont:[UIFont fontWithName:@"TimesNewRomanPS-BoldMT" size:22]
constrainedToSize:CGSizeMake(320, CGFLOAT_MAX)];
Upvotes: 0
Reputation: 4733
For dynamic tableview height
. You need to apply dynamic frame.
For ex,
myTable.frame = CGRectMake(x,y,w,[row count] * 44);
Where 44 is default size of tableview cell
Thus, you need to know each cell height, In order to apply this.
Enjoy Programming
Upvotes: 1
Reputation: 604
Call this method before creating a table view in view did Load. It calculates the Height of the content and and returns total height of table view
-(NSInteger)calculateheightOfText{
for (int i=0; i<[yourarray count]; i++) {
NSString *cellText =[yourarray objectAtIndex:i];
UIFont *cellFont = [UIFont fontWithName:@"ArialMT" size:17.0];
CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
heightOfTableview = heightOfTableview+labelSize.height + 20;
}
heightOfTableview = heightOfTableview +([items count]*35);
return heightOfTableview;
}
Upvotes: 0
Reputation: 413
You try the following code
CGRect tblFrame = [tableView frame];
[tableView setFrame:CGRectMake(tblFrame.origin.x,
tblFrame.origin.y,
tblFrame.size.width,
tblFrame.size.height + 20)];
Upvotes: 0