views:

142

answers:

2

I have a UITableViewController. In viewDidLoad I set the rowHeight:

self.tableView.rowHeight = 43;

But then in cellForRowAtIndexPath, I check the height of the cell:

NSLog(@"bounds: w = %f, h = %f", cell.bounds.size.width, cell.bounds.size.height);

This prints a height of 44 and a width of 320. Anyone know why it would print a height of 44 instead of 43?

Thanks!

A: 

Do you have a:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

You can implement this method to return 43 no matter what. You can also customize to your heart's content. That's why it takes a UITableView as its first argument. You could have multiple UITableViews using the same delegate.

Rob Jones
no, i do not have that function
Tony
+4  A: 

Simply put, the two things are independent of each other.

When you set rowHeight in your UITableView object, you are telling it how to render the table. The default value for rowHeight is 44.

When you create a UITableViewCell object, as a subclass of UIView, it has its own default frame and bounds, which includes a height (and width). The default value for height also just happens to be equal 44.

Your confusion arises because you have created a UITableViewCell object and you expected it to have a height equal to the (not default) rowHeight property in your UITableView object. How can it? It just came into existence!

Like all UIViews, until something comes along and explicitly changes its height, its height won't change.

Shaggy Frog
Not sure I understand. I explicitly set the rowHeight to 43. Therefore I would think the default height of a cell should be 43 so it fits into a row. What if I wanted a really compact table and I set the rowHeight to 10? The cells obviously should not be created at a height of 43.
Tony
There is no connection between your UITableView and the UITableViewCell you are creating. Note how all the delegate methods take a UITableView parameter. That is so *you* can customize how cells are created.
Rob Jones