3

I have a view controller that contains a table view (see screenshot below). This table view always has exactly 3 rows, and these rows expand when clicked to show more information. How can I make it so that the table view's height always shows exactly these three rows, and no more? As you can see in the screenshot, empty rows with no content are being shown.

Note that the expanded rows may contain views other than UILabels.

Here's how it looks in IB:

enter image description here

1
  • How to do this depends somewhat on your design. How are you expanding the cells? Do you allow more than one cell to be expanded at once? Commented Oct 11, 2012 at 3:42

6 Answers 6

5

I think you need something like this

//Return number of rows in section
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    [self setTableHeight];
    return 3;
}

//Method which lets you calculate the height of tableView depending on your 3 cells
- (void)setTableHeight
{

    if(_isRowExpanded == NO)
    {
        tableHeightCalculated = rowHeight * 3; // rowHeight use default 44 or ur custom
    }
    else
    {
        //You need to put some logic here to Calculate or put approx height of  Expanded_Row_Height
        tableHeightCalculated = rowHeight * 2 + Expanded_Row_Height;

        if(tableHeightCalculated > maxHeightAllowed) //maxHeightAllowed is your current table height in snapshot
        {
            tableHeightCalculated = maxHeightAllowed;
        }
    }
    tableView.frame = CGRectMake(tableView.frame.origin.x, tableView.frame.origin.y,
                                 tableView.frame.size.width,  tableHeightCalculated);
}
Sign up to request clarification or add additional context in comments.

Comments

0

If there is more space vertically than what is needed for displaying the actual cells, UITableView inserts fake empty cells. You can't really control this behavior, the most you can do is setting the height of your table view to something sufficiently smaller.

4 Comments

So how can I calculate the appropriate height?
But the height of one cell is dynamic, not some set value.
@JamesHarpe heard of variables? You can reload the table view and then it will ask its delegate again for table cell height data.
How can I determine what height to return for a row if that cell hasn't been loaded yet? If I could figure this out, I could use it to set the height of the whole table.
0

Use the frame property of any control that you want to manipulate via programming. frame has further two properties of size and origin. Using the size property you can manipulate width and height and using the origin you can manipulate x and y property of the control. What you want can be done through:

CGRect frame = control.frame; //control is your UIControl

frame.size.height = 10.0; //suppose 10 is the desired height.

control.frame = frame;

Hope this is what you are looking for.

9 Comments

How can I set this height value dynamically to the height of my three rows? How do I calculate this height?
if you want to set the height for the rows then the method you are looking for is: - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath Using your indexPath.row, you can return any height that you want!
This doesn't help me set the height of the table. How can I calculate the height that the table view should have based on the height of my three rows?
why don't you set its height from the XIB file. Don't you think that would be easy for you to reduce the height from there. Default height of row is 44 so just put it 132 or something or is there something that either of us is missing in the details.
Yes, I could set an intial height of 132, but when a row is clicked, the cell will expand to show more views. I don't know the height of these views. So I need a way to get the new height of the cell after it expands and update the table height appropriately
|
0

try initializing the table´s footerview:

TableView.tableFooterView = [[UIView alloc] init];

1 Comment

This removes the empty cells, but does not change the height of the table. Effectively, it has the appearance of removing the extra lines in the above screenshot.
0

Add this footerview method, This will work. This will remove the empty cells.

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].applicationFrame.size.width, 0)];
v.backgroundColor = [UIColor clearColor];
[tableView setTableHeaderView:v];
[tableView setTableFooterView:v];
return v;
}

1 Comment

Yes, this removes empty cells, but does not change the height of the table. It leaves a bunch of empty white space below my rows.
0

This two lines work for me in the cases that I want the table looking exactly with the content size. It's an "illusion" but works like a charm.

 self.tableView.tableFooterView = [UIView new];
 self.view.backgroundColor = [UIColor whiteColor];

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.