How to make UITableViewCell have variable height
2010-03-01 17:53
393 查看
You have seen variable height
So how to implement this in your own iPhone application?
After you created a
So if you want
There are four
To calculate the exact height of a text block, you’ll need to specify a large
Then you can get the height by accessing
As you’ve seen,
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 in
UITableViewCellin iPhone applications like Twinkle, App Store. These applications have a
UITableViewas 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 created a
UITableView, you will need to set a
delegateand a
datasource. There is a
UITableViewDelegatemethod to tell UITableView how tall a cell would be:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
So if you want
UITableViewCellto 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 four
NSStringaddition 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 large
CGSizeand 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 accessing
size.height.
As you’ve seen,
UITableViewin iPhone cannot generate dynamic height automatically, 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 in
CGSizeMake, there are 10 pixels for padding in left and right side of text.
相关文章推荐
- How to make UILabel / UITableViewCell to have a dynamic height
- how to set UITableViewCell background image
- How to get UITableView from UITableViewCell?
- how to register a cell for UITableViewCell
- How to get UITableView from UITableViewCell?
- ValueError: Attempt to have a second RNNCell use the weights of a variable scope already has weights
- How to customize the background color of a UITableViewCell? 修改UITableViewCell背景色
- How to make cell editable dynamically in jqGrid
- How to make omnetpp.ini variable randomized
- how to make custom table cell
- How to customize the background color of a UITableViewCell?
- How to stop UITableView from clipping UITableViewCell contents in iOS 7
- how to make PKG_CONFIG_PATH variable to refer an installed library?
- bzr: ERROR: These branches have diverged. Use the missing command to see how.
- How to make a call in Windows Phone 7 application
- How to Make a AutoBuild System with Git Server
- How to make CUDA 5.5 nvcc run under 64bit Ubuntu
- think about how to make the function close to really world
- How To Make KeyGens
- How to Make an AiO [Reloaded Edition] by vertigo173