M Ali Çalışkan
M Ali Çalışkan

Reputation: 41

Setting variable height of a UITableViewCell based on its width - IPhone

In IPhone tableview content width is either 320 or 300 depending on the style. However when Ipad is on the stage, we started to use tableviews in different widths according to our design.

One problem is, in some cases we calculate the height of a cell according to its subtitle text size. The text height depends to the contentwidth, while the cell height depends on the text height. If the width is 300 or 320 it is ok. But think that I use a 500 pix wide grouped style tableview, and there is no way to calculate reduced content width. Because of this problem we can not calculate the height of the call depending to its content.

The only location where we can get an information about the contentwidth of the cell is a subclassed layoutsubview method. However the heightForRowAtIndexPath is called before layoutSubview method and we dont have the information about the content width of the reduced cell.

So we need a good way to calculate the true width of a grouped style tableview cell.

I will be glad for any help.

Thanks.

M Ali Caliskan

Upvotes: 4

Views: 1428

Answers (4)

mehmed.ali
mehmed.ali

Reputation: 362

At last I solved the width issue of a grouped tableView. The padding size which is 10 (in one side) for the standart 320 width varies according to the width through an insteresting math. The simple way to handle this math, is to use fixed values according to the width ranges. Below code is valid for 4.0 and I didn't tested it on any other iOs version.

Please multiply the returned padding size by 2, to find the total reducement of a grouped style tableView. For example if the paddign size is 10 for a 320 pix wide tableView, the contentWidth is 320 - (2.10) = 300.

CGFloat GetTableViewPaddingSize(CGFloat tableViewWidth)
{
    if (tableViewWidth < 401) {
        return 10;
    } else if (tableViewWidth < 547) {
        return 31;
    } else if (tableViewWidth < 560) {
        return 32;
    } else if (tableViewWidth < 573) {
        return 33;
    } else if (tableViewWidth < 586) {
        return 34;
    } else if (tableViewWidth < 599) {
        return 35;
    } else if (tableViewWidth < 612) {
        return 36;
    } else if (tableViewWidth < 625) {
        return 37;
    } else if (tableViewWidth < 639) {
        return 38;
    } else if (tableViewWidth < 652) {
        return 39;
    } else if (tableViewWidth < 665) {
        return 40;
    } else if (tableViewWidth < 678) {
        return 41;
    } else if (tableViewWidth < 691) {
        return 42;
    } else if (tableViewWidth < 704) {
        return 43;
    } else if (tableViewWidth < 717) {
        return 44;
    } else {
        return 45;
    }
}

Upvotes: 1

Rahul Sharma
Rahul Sharma

Reputation: 3013

As James mentioned and explained, you can use something like

UIFont *font = [UIFont systemFontOfSize:16];    
CGSize size = [TEXT_STRING sizeWithFont:font constrainedToSize:CGSizeMake(tableView.frame.size.width, 16*1000.f) lineBreakMode:UILineBreakModeCharacterWrap];
return size.height;

Use this is in delegate heightForRowAtIndexPath: as well as in cellForRowAtIndexPath: to return the height for tablecell and to update the cell frame.

Hope this might be of some help to you.

Upvotes: 0

James Bedford
James Bedford

Reputation: 28982

The NSString UIKit Additons (found here) provides many methods for finding the size of a string given a particular UIFont and line break mode (remember the String, UIFont etc. can come from your model classes, so this has all known before the UITableView is required to be displayed). You could use these methods in conjunction with your own personal spacing/padding/etc. in order to find out the desired size (thus the width and height) of the UITableViewCell at the time when heightForRowAtIndexPath: is called.

So long as you make a decision as to how you will calculate the size for a given table cell, and then keep this method consitent throughout all the UITableView delegate and datasource methods, then you should be fine.

Upvotes: 0

Daniel Dickison
Daniel Dickison

Reputation: 21882

I had a similar problem, namely that the width of a table cell's contentView is not known when heightForRowAtIndexPath is called if you have an accessory view. In the end, there was no solution except to hard-code the width of the accessory view (20px) and subtract it from the usual width. I think you will have to do the same thing -- hard-code the cell content width depending on the full width of the table view.

Upvotes: 0

Related Questions