【问题标题】:Cell size not changing dynamically单元格大小不会动态变化
【发布时间】:2014-07-03 07:03:48
【问题描述】:

我正在使用 UITableView 创建一个 Iphone 应用程序。在那我想改变相对于文本的单元格大小。我使用了以下代码。但它失败了。

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//    CGRect screenBounds = [[UIScreen mainScreen] bounds];
//    CGSize screenSize = screenBounds.size;

NSStringDrawingContext *ctx = [NSStringDrawingContext new];
NSAttributedString *aString = [[NSAttributedString alloc] initWithString:[message objectAtIndex:0]];
UITextView *calculationView = [[UITextView alloc] init];
[calculationView setAttributedText:aString];
CGRect textRect = [calculationView.text boundingRectWithSize:self.view.frame.size options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:calculationView.font} context:ctx];
//    CGSize size = [calculationView sizeThatFits:CGSizeMake(screenSize.width, FLT_MAX)];
return textRect.size.height;
}



-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"SimpleTableCell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
[cell.textLabel setLineBreakMode:NSLineBreakByWordWrapping];
[cell.textLabel sizeToFit];

if(cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.imageView.image = [UIImage imageNamed:@"gg.jpg"];
cell.textLabel.text = [NSString stringWithFormat:[message objectAtIndex:0], indexPath.row];
return cell;
}

提前致谢。

【问题讨论】:

  • 您可能想要 NSLog() textRect.size.height 值的高度以查看您得到的高度。您在视觉上看到了什么,您的单元格高度是否正确间隔但文本是单行?如果是,请尝试设置 cell.textLabel.numberOfRows = 0;否则,如果您愿意接受替代方法,我在这里使用 Autolayout 编写了一个演示应用程序:stackoverflow.com/questions/24478825/…

标签: ios iphone uitableview ios7


【解决方案1】:

首先你需要获取文本大小..然后根据这个大小你可以设置单元格高度..就像这个例子

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


            CGSize contsize={260.00f,3000.00f};
            UIFont *font=[UIFont fontWithName:@"Open Sans" size:15.0];

            NSString *strt11=@"comment_text"

            CGSize fontSize=[test sizeWithFont:font constrainedToSize:contsize lineBreakMode:NSLineBreakByWordWrapping];        
            if (fontSize.height>22)
                return fontSize.height+60.0f;

            return 80.0f;
    }

并将此代码放入cellForRowAtIndexPath 方法中以设置 uilabel 高度..

【讨论】:

    【解决方案2】:

    请使用以下代码。

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    
    
        NSString *text = [array_loaddata objectAtIndex:indexPath.row] ;
    
            UIFont *font = [UIFont systemFontOfSize:10];
        NSAttributedString *attributedText =
        [[NSAttributedString alloc]
         initWithString:cellText
         attributes:@
         {
         NSFontAttributeName: cellFont
         }];
        CGRect rect = [attributedText boundingRectWithSize:(CGSize){kWIDTH_FOR_MESSAGE_LABEL, MAXFLOAT}
                                                   options:NSStringDrawingUsesLineFragmentOrigin
                                                   context:nil];
    
    
        return rect.size.height + 75;
    }
    
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        NSString *CellIdentifier =[NSString stringWithFormat:@"Cell%d",indexPath.row] ;
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    
            NSString *text = [array_loaddata objectAtIndex:indexPath.row] ;
            UIFont *font = [UIFont systemFontOfSize:10];
        NSAttributedString *attributedText =
        [[NSAttributedString alloc]
         initWithString:cellText
         attributes:@
         {
         NSFontAttributeName: cellFont
         }];
        CGRect rect = [text boundingRectWithSize:(CGSize){kWIDTH_FOR_MESSAGE_LABEL, MAXFLOAT}
                                                   options:NSStringDrawingUsesLineFragmentOrigin
                                                   context:nil];  
          UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(46, 0,kWIDTH_FOR_MESSAGE_LABEL, rect.size.height)];
            label.numberOfLines = 0;
            label.textColor = [UIColor grayColor];
            label.lineBreakMode = NSLineBreakByWordWrapping;
            label.text = (text ? text : @"");
            label.font = font;
            label.backgroundColor = [UIColor clearColor];
            [cell.contentView addSubview:label];
            [label release];
    
    
        }
    

    【讨论】:

    • 以上内容只会增加文本未完全显示的单元格高度。
    • 对于 kWIDTH_FOR_MESSAGE_LABEL 给出您所需的标签宽度。
    【解决方案3】:

    就这么简单:

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
         return UITableViewAutomaticDimension;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-07-08
      • 1970-01-01
      • 1970-01-01
      • 2012-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多