自定义UITableView动态单元格高度

时间:2011-09-28 20:01:14

标签: iphone uitableview uitextview

我搜索并搜索了无数的博客和文章,了解如何确定自定义UITableViewCell的动态高度及其详细文本。我真的很难找到任何关于此的好文档。

我需要做的是让细胞根据里面的文字生长,但绝不要低于70的高度。

我已经在StackOverflow上尝试了这类问题的几个答案,但没有一个能够工作。我的整个应用程序即将完成,但我确实需要在发布之前完成这项工作并且很麻烦。

这是我正在尝试的但我只是得到一堆细胞相互重叠。根据我的阅读,我需要找到框架,如果单元格中的自定义单元格或textview,但我不知道如何做到这一点或将它们混合在一起以返回一个高度。

非常感谢任何帮助谢谢!

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

  CGSize aSize;
aSize = [[(Tweet*)[tweets objectAtIndex:indexPath.row]tweet] sizeWithFont:[UIFont 
systemFontOfSize:14]
            constrainedToSize:CGSizeMake(300.0, 1000.0)
                lineBreakMode:UILineBreakModeTailTruncation];

return  aSize.height;
}

6 个答案:

答案 0 :(得分:18)

我有一段时间有类似的问题,这对我帮助很大。

#define PADDING 10.0f
- (CGFloat)tableView:(UITableView *)t heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *text = [self.items objectAtIndex:indexPath.row];
    CGSize textSize = [text sizeWithFont:[UIFont systemFontOfSize:14.0f] constrainedToSize:CGSizeMake(self.tableView.frame.size.width - PADDING * 3, 1000.0f)];

    return textSize.height + PADDING * 3;
}

答案 1 :(得分:3)

嘿,所以你需要在NSArray中存储字符串列表,然后你需要使用sizeWithFont来计算nsstring的高度:constrainedToSize:文档在这里http://developer.apple.com/library/ios/#documentation/UIKit/Reference/NSString_UIKit_Additions/Reference/Reference.html < / p>

所以你的tableView方法看起来应该像

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *stringAttributes = [NSDictionary dictionaryWithObject:[UIFont fontWithName:"Helvetica" size:9] forKey: NSFontAttributeName];

     CGSize cell_size = [string boundingRectWithSize:CGSizeMake(300,999) 
                            options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin     
                            attributes:stringAttributes context:nil].size;
if (cell_size.height > 70)
{
return cell_size.height;
}
else 
{
return 70;
}
}

编辑:这已针对iOS 7进行了更新

答案 2 :(得分:0)

我刚刚写了这个问题以及我最终决定如何解决它。您可以在此处阅读:Dynamic UITableView Cell Height Based on Contents

基本上,我创建了一个UITableView子类,它可以自动处理和计算默认和自定义单元格的动态单元格高度。它不是一个银弹,可能需要扩展和调整,但我已经在几个应用程序中使用它,结果很好。

您可以在此处获取代码:https://github.com/danielsaidi/AutoSizeTableView

希望它有所帮助!

(......如果没有,我很想知道为什么不这样做)

答案 3 :(得分:0)

我尝试了很多解决方案,但有一个解决方案就是这个,由朋友建议:

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

    int height = [StringUtils findHeightForText:yourLabel havingWidth:yourWidth andFont:[UIFont systemFontOfSize:17.0f]];

    height += [StringUtils findHeightForText:yourOtherLabel havingWidth:yourWidth andFont:[UIFont systemFontOfSize:14.0f]];

    return height + CELL_SIZE_WITHOUT_LABELS; //important to know the size of your custom cell without the height of the variable labels
}

StringUtils.h类:

 #import <Foundation/Foundation.h>

    @interface StringUtils : NSObject

    + (CGFloat)findHeightForText:(NSString *)text havingWidth:(CGFloat)widthValue andFont:(UIFont *)font;

    @end

StringUtils.m class:

    #import "StringUtils.h"

    @implementation StringUtils

    + (CGFloat)findHeightForText:(NSString *)text havingWidth:(CGFloat)widthValue andFont:(UIFont *)font {
        CGFloat result = font.pointSize+4;
        if (text) {
            CGSize size;

            CGRect frame = [text boundingRectWithSize:CGSizeMake(widthValue, CGFLOAT_MAX)
                                              options:NSStringDrawingUsesLineFragmentOrigin
                                           attributes:@{NSFontAttributeName:font}
                                              context:nil];
            size = CGSizeMake(frame.size.width, frame.size.height+1);
            result = MAX(size.height, result); //At least one row
        }
        return result;
    }

    @end

它对我来说很完美。我有一个自定义单元格,其中包含3个固定尺寸的图像,2个固定尺寸的标签和2个可变标签。工作就像一个魅力。希望它也适合你。

最好的问候,亚历山大。

答案 4 :(得分:0)

iOS 7

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    NSString *text = @"Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello HelloHello HelloHello HelloHello HelloHello HelloHello Hello";

    CGRect rect = [text boundingRectWithSize:CGSizeMake(280.0f, CGFLOAT_MAX) options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin attributes:@{
        NSFontAttributeName: [UIFont fontWithName:@"Hellvetica" size:14.0f]
    } context:nil];

    return CGSizeMake(320.0f, ceil(rect.size.height) + 10.0f);
}

答案 5 :(得分:-4)

使用UITableViewDelegate方法:

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
 // here you need to get height of your textlabel in your custom cell. 
 MyCustomCell *myCell = (MyCustomCell *)[tableView cellForRowAtIndexPath:indexPath];
 return myCell.myTextLabel.frame.size.height + 20;
}

像这样的东西

相关问题