vivek bhoraniya
vivek bhoraniya

Reputation: 1535

Set dynamic height of tableview

I have one tableview in my screen. I want to show that tableview in max that height only but I want to set its height according to row. If there is two row then it height should be up to two row. Other part of table view should not be shown.

enter image description here

You can see right now there is two row only but table height is more so it is showing that part.I want to set table height dynamically. I dont know how to set. I also google it but though I dont find any solution

Upvotes: 0

Views: 237

Answers (3)

Zeeshan
Zeeshan

Reputation: 4244

I did it like this : a UITableView whose height = Screen height - Navigationbar height. i-e

height = self.view.frame.size.height-64

self.dataArrayForCategory is our Datasource and 44 is our RowHeight.
[self.tblHomeMenu setFrame:CGRectMake(0, 0, 320,
44*self.dataArrayForCategory.count)];
(self.tblHomeMenu.frame.size.height>height)?[self.tblHomeMenu
setFrame:CGRectMake(0, 0, 320,
height)]:[self.tblHomeMenu
setFrame:CGRectMake(0, 0, 320, 44*self.dataArrayForCategory.count)];
if (self.tblHomeMenu.frame.size.height>self.view.frame.size.height-64)
{
    [self.tblHomeMenu setScrollEnabled:YES];
}

I had a UITableView whose height is Screensize - Navigationbar.

last line is used if UITableView height > Screensize - Navigationbar then UITableView should be scrollable else it should not.

in your case your height will be height of your UITableView.

Upvotes: 2

Gyanendra Singh
Gyanendra Singh

Reputation: 1483

Get the number of row first and then multiply it by height of individual row.

float height = (no_of_row) * row_height + header_height; // 4*25.00 + 10

CGRect frame = CGRectMake(self.table.frame.origin.x, self.table.frame.origin.y, self.view.frame.size.width, height);

self.table.frame =frame;

Upvotes: 1

Ashutosh
Ashutosh

Reputation: 2225

Use below code:

float height=27.0f*[yourArray count];
if (height > MaxHeight)
{
    height = MaxHeight;
}

[yourTableView setFrame:CGRectMake(yourTableView.frame.origin.x, yourTableView.frame.origin.y,yourWidth,height)];

Upvotes: 0

Related Questions