UITableViewCell动态高度:/ [英] UITableViewCell dynamic height :/

查看:138
本文介绍了UITableViewCell动态高度:/的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

呃,所有的,

我已经创建了一个带有NIB文件的UITableViewCell。
它有一个标签,它将包含一个鸣叫。所以它需要一个动态的高度。还有一个timeAgo标签必须贴在tweet标签下面。

I have created a UITableViewCell with a NIB file. There is 1 label in it wich is going to contain a tweet. So it needs to be a dynamic height. There also is a timeAgo label that has to fit underneath the tweet label.

我正在尝试使用框架尺寸的东西,但是我无法获得完美的解决方案。
我在drawable方法的UITableViewCell文件中执行此操作。

I'm trying stuff with frames en sizes but i can't get the perfect solution.. I do this in the UITableViewCell file in the drawrect method.

self.tweet.lineBreakMode = UILineBreakModeWordWrap;
self.tweet.numberOfLines = 0;
self.tweet.font = [UIFont fontWithName:@"Arial" size:13.0f];
[self.tweet sizeToFit];  

CGFloat tweetHeight = self.tweet.frame.size.height;

self.timeAgo.lineBreakMode = UILineBreakModeWordWrap;
self.timeAgo.numberOfLines = 0;
self.timeAgo.font = [UIFont fontWithName:@"Arial" size:11.0f];
[self.timeAgo sizeToFit];

CGFloat timeAgoHeight = self.timeAgo.frame.size.height;

self.timeAgo.frame = CGRectMake(88, tweetHeight, 100, timeAgoHeight + 10.0f);

我也在一个教程中找到了一个stringhelper。

I have also tried a stringhelper wich i found in a tutorial.

- (CGFloat)RAD_textHeightForSystemFontOfSize:(CGFloat)size {

我的HeightForRow方法也已经不同了,因为我使用不同的单元格样式。
此刻,我为每个单元格样式返回一个硬值,但也需要更改为单元格。

My HeightForRow methods is also already different because i use different cell styles. At the moment i return a hard value for each cell style but that also needs to change to the cellheight.

希望你们能指出我的方向。

Hope you guys can point me in a good direction.

谢谢,


  • Rolf

推荐答案

本教程, http://www.cimgf.com/2009/09 / 23 / uitableviewcell-dynamic-height /

诀窍是使标签随着单元格的大小而增长,而不仅仅是设置尺寸

The trick is to make the label grow with the size of the cell, than you can just set the size of the cell and the cell will grow with it.

设置timeAgo标签使其自身与单元格底部对齐。

Set the timeAgo label to align it self to the bottom of the cell.

通过IB将tweet的numberOfLines设置为0,重新移动所有的绘制代码,只执行以下操作:

Set the numberOfLines of tweet to 0 via IB,re move all the draw code and only implement the following:

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    id item  = [self.item objectAtIndex:indexpath.row];

    CGFloat height = 85.0f;

    if ([item isKindOfClass:[Tweet class]]) {
        Tweet *tweet = (Tweet *)item;
        CGSize titleSize = [tweet.tweet sizeWithFont:[UIFont fontWithName:@"Arial" size:13.0f] constrainedToSize:CGSizeMake(260.0f, MAXFLOAT)];

        // adde the 24 pixels to get the height plus the time ago label.
        height =  titleSize.height + 24.0f;

    } else if( [item isKinfOfClass:[SC_Release class]]) {
        height = 65.0f;
    }

   return height;
}

这篇关于UITableViewCell动态高度:/的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆