自定义UITableView动态单元格高度



关于如何确定自定义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;
}

我之前也遇到过类似的问题,这对我帮助很大。

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

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

所以你的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

我尝试了许多解决方案,但最有效的是一个朋友建议的:

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

stringutil的。m类:

    #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个可变标签。效果很好。希望它对你也有用。

致以最诚挚的问候,Alexandre

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

我刚刚写了这个问题以及我最终决定如何解决它。你可以在这里阅读它:动态UITableView单元格高度基于内容

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

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

希望有帮助!

(…如果没有,我很想知道为什么)

使用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;
}

像这样

最新更新