开发者

UITableView cell height setting life-cycle

Trying to understand when the cell height is set. I 开发者_如何学编程have a UITableView that is created in layout, not code.

Then:

#define TABLE_VIEW_ROW_HEIGHT   100.0

Followed by:

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"Setting row height");
    return TABLE_VIEW_ROW_HEIGHT;
}

and

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"Row height: %f", tableView.rowHeight);
    // ... other happy stuff that does not change row height but could sure use the real row height

The last NSLog reports 44 rather than 100. Which means that the code within that method that needs to get the row height is not using the real height.

I've verified that

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

runs before

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

And, as a result, the rows are of the desired size, not the default size of 44.

Do I need to explicitly set the height somewhere else?

EDIT AFTER DOING SOME RESEARCH:

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

...is more appropriately used to set the height of rows when not all rows are of the same height. I was wrongly using this to set all rows to the same height.

The right way to do it is to call

the_tableview.rowHeight = TABLE_VIEW_ROW_HEIGHT;

...from somewhere like viewDidLoad. I actually did that (code omitted) but it didn't work. Tonight I realized that this is yet another instance of being had by the graphical layout tool...I forgot to connect the IBOutlet for the UITableView...so, my code did nothing and the entire tableview remained at the default height of 44. That's why I was getting 44 out of my NSLog.


In your code, you are calling the rowHeight on tableView and not on a specific row at IndexPath. That is the reason why you are getting the default value of 44. If you want to change the default value then you can do it in size inspector for the table. Changing the default value is better if all the rows have the same height. If there are rows with different heights then you will have to implement heightForRowAtIndexPath for that the rows with different heights.

You have defined TABLE_VIEW_ROW_HEIGHT in code then in this case you will also have to set the tableView.rowHeight in code by assigning it the defined TABLE_VIEW_ROW_HEIGHT name.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜