2011-09-28 89 views
7

我有搜索和搜索无尽的博客和文章如何确定自定义UITableViewCell的动态高度及其详细文本。我真的很难找到任何有关这方面的优秀文档。自定义UITableView动态单元格高度

我需要做的是根据文本的细胞内生长,但从来没有去的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; 
} 
+0

http://imageshack.us/photo/my-images/508/screenshot2011092816313.png – FreeAppl3

+0

的图像显示了我与几乎所有的方法我试试!它似乎是获取每个文本字符串的大小,但不是调整单元格大小 – FreeAppl3

回答

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; 
} 
+0

我必须用“PADDING * 4”替换第一个“PADDING * 3”,后面的“PADDING * 3”用“PADDING * 2”替换...... –

+0

是的你将需要根据您的具体需求进行调整,但这只是一个基础。 – WrightsCS

+5

你应该注意'sizeWithFont:constrainedToSize:'在iOS 7中已被弃用,所以你应该使用'boundingRectWithSize:options:attributes:context:' –

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

像这样的事情

+0

第一行代码[tableView cellForRowAtIndexPath:indexPath];只是使应用程序崩溃....感谢您的答复,虽然 – FreeAppl3

+0

它崩溃,因为你还没有细胞。我没有想到,对不起。 WrightsCS答案应该有帮助 –

+0

它被压坏,“EXE_BAD_ACCESS” – HelloWorld

3

嘿所以你将需要字符串列表存储在一个NSArray,然后你将需要使用sizeWithFont计算nsstring的高度:constrainedToSize:在此处找到文档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

0

我只是写有关此问题以及如何我终于决定来解决它。你可以在这里阅读:Dynamic UITableView Cell Height Based on Contents

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

你可以在这里抢码:https://github.com/danielsaidi/AutoSizeTableView

希望它能帮助!

(...如果没有,我很想听听为什么不)

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的。米类:

#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个可变标签。像魅力一样工作。希望它也适用于你。

最好的问候,亚历山大。

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