4

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

4 Answers4

1

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;
    }
}
Brad Larson
  • 170,088
  • 45
  • 397
  • 571
mehmed.ali
  • 252
  • 1
  • 4
0

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.

Community
  • 1
  • 1
Daniel Dickison
  • 21,832
  • 13
  • 69
  • 89
0

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.

James Bedford
  • 28,702
  • 8
  • 57
  • 64
  • Thanks for your answer, but my problem is not about the proper function for finding the height of string. We can find the height of the sitring if only we know the width of it. The problem is that when we use grouped tableview we dont know the content width since it is reduced. In Iphone 320 is reduced to 300. – M Ali Çalışkan Feb 04 '11 at 19:10
  • So according to the tableview style we had handled this size as hardcoded. But in Ipad we use tableviews in many widths. For example what will be the real content width of a grouped 500 pix wide tableview. The reduce delta is not always 20 pix. iOs does some inner math to calculate the reduce delta. I just want to know it for any give width. – M Ali Çalışkan Feb 04 '11 at 19:11
  • Have you checked the frame of the UITableViewCell's contentView? My understanding of grouped TableView cells as opposed to non-grouped was that the frame of the cell didn't actually change. – James Bedford Feb 04 '11 at 19:44
0

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.

Rahul Sharma
  • 3,013
  • 1
  • 20
  • 47