How to Make uitableviewcell have variable height

Source: Internet
Author: User

You have seen variable heightUITableViewCellIn iPhone applications like twinkle, App Store. These applications haveUITableViewAs main part of UI, each cell in the table has a variable height according to text amount it holds.

So how to implement this in your own iPhone application?

After you createdUITableView, You will need to setdelegateAnddatasource. There isUITableViewDelegateMethod To tell uitableview how tall a cell wocould be:

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

So if you wantUITableViewCellTo have variable height like twinkle, you'll need to calculate the height of text block and return the result in method I mentioned above.

There are fourNSStringAddition methods can do the calculations:

- (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size
- (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size lineBreakMode:(UILineBreakMode)lineBreakMode
- (CGSize)sizeWithFont:(UIFont *)font forWidth:(CGFloat)width lineBreakMode:(UILineBreakMode)lineBreakMode
- (CGSize)sizeWithFont:(UIFont *)font minFontSize:(CGFloat)minFontSize actualFontSize:(CGFloat *)actualFontSize forWidth:(CGFloat)width lineBreakMode:(UILineBreakMode)lineBreakMode

To calculate the exact height of a text block, you'll need to specify a largeCGSizeAnd those methods will return you exact size, here's an example:

struct CGSize size;
size = [aString sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(300.0, 4000) lineBreakMode:UILineBreakModeCharacterWrap];

Then you can get the height by accessingsize.height.

As you 've seen,UITableViewIn iPhone cannot generate dynamic heightAutomatically, You'll need to calculate the height yourself before you set the delegate. This is quite different if you're coming from a HTML world.

If you 've found a better way to implement this, please share with us in comments. Thanks.

P.s. Don't forget to have some padding around text. And what's why I use 300 as width inCGSizeMake, There are 10 pixels for padding in left and right side of text.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.