Reputation: 2361
I have a UITableView with a variable number of rows (cells) - the height of these rows is constant. What I would like is to make the height of the UITableView depend on the number of rows.
Also, I would like the UITableView to be exactly wrapping the cells, so no padding at the bottom. So if a cell has height 60, tableview should be 60 for one cell, 120 for two cells, etc...
Thanks in advance!
Upvotes: 14
Views: 27487
Reputation: 7154
You could use the delegate method tableView:numberOfRowsInSection:
to set the UITableView height dynamically, like this:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
CGFloat numberOfRows = self.myArray.count;
CGRect tableViewFrame = tableView.frame;
tableViewFrame.size.height = numberOfRows * tableView.rowHeight;
tableView.frame = tableViewFrame;
return numberOfRows;
}
Remember also to define the rowHeight
where you initiate the table view:
self.myTableView.rowHeight = 60.0;
... but if your goal is just to hide empty cells in the table, you should add an empty view to it's tableFooterView
instead and ignore all the code above (so try this line first):
self.myTableView.tableFooterView = [UIView new];
Upvotes: 0
Reputation: 849
I made a function to set the uitableview height as per @timvermeulen's code
-(void)setTableViewheightOfTable :(UITableView *)tableView ByArrayName:(NSArray *)array
{
CGFloat height = tableView.rowHeight;
height *= array.count;
CGRect tableFrame = tableView.frame;
tableFrame.size.height = height;
tableView.frame = tableFrame;
}
Upvotes: 2
Reputation: 12562
You could access your data source to find the number of cells that will be displayed. Multiply this number by the height of one row and you get the height of the entire table view. To change the height of a table view, you can change its frame property.
You can access the (constant) height of one row of your table view by accessing its rowHeight
property. If you fill your table view with the objects of an array called myArray
, you could use the following code:
CGFloat height = self.tableView.rowHeight;
height *= myArray.count;
CGRect tableFrame = self.tableView.frame;
tableFrame.size.height = height;
self.tableView.frame = tableFrame;
You could also find out how many rows the table view will contain by asking the table view itself, instead of the data object. Something like this should work:
NSInteger numberOfCells = 0;
//finding the number of cells in your table view by looping through its sections
for (NSInteger section = 0; section < [self numberOfSectionsInTableView:self.tableView]; section++)
numberOfCells += [self tableView:self.tableView numberOfRowsInSection:section];
CGFloat height = numberOfCells * self.tableView.rowHeight;
CGRect tableFrame = self.tableView.frame;
tableFrame.size.height = height;
self.tableView.frame = tableFrame;
//then the tableView must be redrawn
[self.tableView setNeedsDisplay];
Upvotes: 29
Reputation: 2453
Implement this UITableView delegate method . Whenever a cell row create it assign height to each cell
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 60.0f;
}
and implement this code in viewDidLoad method
[yourTableView setFrame:CGRectMake(yourTableView.frame.origin.x, yourTableView.frame.origin.y, yourTableView.frame.size.width,(60.0f*([yourArray count])))];
Note :- yourArray is an NSArray which contain data that how many row you want to crate.
OR
If yourArray fetching value dynamically then call this method after fetching value.
-(void)setHeightOfTableView
{
/**** set frame size of tableview according to number of cells ****/
float height=60.0f*[yourArray count];
[yourTableView setFrame:CGRectMake(yourTableView.frame.origin.x, yourTableView.frame.origin.y, yourTableView.frame.size.width,(60.0f*([yourArray count])))];
}
I hope it works. Thanks
Upvotes: 2
Reputation:
This is how i solved.
CGFloat height = self.mMyTicketTable.rowHeight;
float h = height * [self.mArrEName count];
if(h > 410.0)
{
self.mMyTicketTable.frame = CGRectMake(0, 50, self.mMyTicketTable.frame.size.width, 410);
}
else
{
self.mMyTicketTable.frame = CGRectMake(0, 50, self.mMyTicketTable.frame.size.width, h);
}
NSLog(@"h:%f", h);
Upvotes: 2