Setting UITableViewCell Variable Height Based on Its Width - IPhone

In IPhone, the width of the contents of the table is 320 or 300, depending on the style. However, when the iPad is on stage, we started using tables in different widths in accordance with our design.

One of the problems is that in some cases we calculate the height of the cell according to its subtitle text size. The height of the text depends on the width of the content, and the height of the cell depends on the height of the text. If the width is 300 or 320, this is normal. But think that I am using a table view of a table with a size of 500 pixels, and there is no way to calculate the reduced width of the content. Due to this problem, we cannot calculate the height of the call depending on its contents.

The only place we can get information about the width of the contents of the cell is a subclass of the layoutsubview method. However, heightForRowAtIndexPath is called before the layoutSubview method, and we have no information about the width of the contents of the reduced cell.

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

I will be happy for any help.

Thanks.

M Ali Kaliskan

+4
source share
4 answers

Finally, I solved the width problem of a grouped tableView. The fill size, which is 10 (on the one hand) for a standard width of 320, varies according to the math width. An easy way to handle this math is to use fixed values ​​according to width ranges. Below code is valid for 4.0, and I have not tested it on any other version of iOs.

Please multiply the returned fill size by 2 to find the overall abbreviation of the grouped tableView style. For example, if the paddign size is 10 for a TableView with a width of 320 pixels, 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; } } 
+1
source

I had a similar problem , namely that the width of the contents of the cell in the View table is unknown when heightForRowAtIndexPath is heightForRowAtIndexPath , if you have an accessory. In the end, there was no solution other than a hard code for the width of the accessory (20px) and subtraction from the usual width. I think you have to do the same thing - hard code the width of the contents of the cell depending on the full width of the table view.

0
source

NSString UIKit Additons (found here ) provides many methods for finding the size of a string given for a specific UIFont mode and interrupting a string (remember String, UIFont, etc. can be obtained from your model classes, so all this is known before the UITableView will need to display). You can use these methods in combination with your own personal interval / addition / etc. to find out the desired size (thus width and height) of a UITableViewCell at the time heightForRowAtIndexPath: called.

As long as you decide how you will calculate the size for a given table cell, and then save this method in accordance with all the parameters of the UITableView delegate and data source, then you should be fine.

0
source

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 in the delegate heightForRowAtIndexPath: as well as in cellForRowAtIndexPath: to return the height for tablecell and update the cell frame.

Hope this can help you.

0
source

Source: https://habr.com/ru/post/1338452/


All Articles