【问题标题】:Custom UITableView Dynamic Cell Height自定义 UITableView 动态单元格高度
【发布时间】:2011-11-27 04:23:13
【问题描述】:

我已经搜索并搜索了无数关于如何确定自定义 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;
}

【问题讨论】:

标签: iphone uitableview uitextview


【解决方案1】:

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

类似的东西

【讨论】:

  • 第一行代码[tableView cellForRowAtIndexPath:indexPath];只是让应用程序崩溃....谢谢你的回复
  • 它崩溃是因为你还没有单元格。我没有想到,对不起。 WrightsCS 的答案应该会有所帮助
  • 它崩溃了,"EXE_BAD_ACCESS "
【解决方案2】:

您好,您需要将字符串列表存储在 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 进行了更新

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

    • 我不得不用“PADDING * 4”替换第一个“PADDING * 3”,用“PADDING * 2”替换后一个“PADDING * 3”......
    • 是的,您需要根据自己的具体需求进行调整,但这只是一个基础。
    • 你应该注意到sizeWithFont:constrainedToSize:在iOS 7中被弃用了,所以你应该使用boundingRectWithSize:options:attributes:context:
    【解决方案4】:

    我刚刚写了关于这个问题以及我最终决定如何解决它的文章。你可以在这里阅读:Dynamic UITableView Cell Height Based on Contents

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

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

    希望对你有帮助!

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

    【讨论】:

      【解决方案5】:

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

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

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

      最好的问候,亚历山大。

      【讨论】:

        【解决方案6】:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-12-03
          • 1970-01-01
          • 2021-09-17
          • 1970-01-01
          • 1970-01-01
          • 2019-08-15
          相关资源
          最近更新 更多