UITableViewCell 动态高度与标签

发布于 2024-12-02 15:25:58 字数 937 浏览 0 评论 0原文

我创建了一个带有 NIB 文件的 UITableViewCell 。 其中有 1 个标签将包含一条推文。所以它需要是一个动态高度。还有一个 timeAgo 标签必须位于推文标签下方。

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

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);

我还尝试了在教程中找到的字符串助手。

我的 HeightForRow方法

- (CGFloat)RAD_textHeightForSystemFontOfSize:(CGFloat)size {

也已经不同,因为我使用不同的单元格样式。 目前,我为每种单元格样式返回一个硬值,但这也需要更改为单元格高度。

I have created a UITableViewCell with a NIB file.
There is 1 label in it which 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.

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);

I have also tried a stringhelper which I found in a tutorial.

The:

- (CGFloat)RAD_textHeightForSystemFontOfSize:(CGFloat)size {

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼泪淡了忧伤 2024-12-09 15:25:59

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

技巧是让标签随着单元格的大小而增长,而不是您只需设置单元格的大小,单元格就会随之增长。

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

通过IB将tweet的numberOfLines设置为0,重新移动所有绘制代码​​,仅实现以下内容:

- (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;
}

See this tutorial, 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.

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

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;
}
桃扇骨 2024-12-09 15:25:59
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *string = [tweetsArray indexPath.row];
    CGSize labelSize = [string sizeWithFont:[UIFont fontWithName:@"Verdana" size:17.0] 
                                                     constrainedToSize:CGSizeMake(280.0f, MAXFLOAT) 
                                                         lineBreakMode:UILineBreakModeWordWrap];
    return labelSize.height + 20;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *string = [tweetsArray indexPath.row];
    CGSize labelSize = [string sizeWithFont:[UIFont fontWithName:@"Verdana" size:17.0] 
                                                     constrainedToSize:CGSizeMake(280.0f, MAXFLOAT) 
                                                         lineBreakMode:UILineBreakModeWordWrap];
    return labelSize.height + 20;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文