【问题标题】:How to increase UItableView Cell and UITextView height based on textlength in ios如何在ios中根据textlength增加UItableView Cell和UITextView高度
【发布时间】:2015-07-21 12:05:04
【问题描述】:

嗨,我是 Ios 的初学者,在我的项目中,我在 UItableview 单元格上添加了 UItextView,并根据 textlength 自动增加 UItextview 和 UItableview 单元格的高度,但根据我的代码,所有 textdata 都没有显示在 textview 上,有些数据丢失了请帮助我某一个

这是我的代码:

 formulaArray = [NSArray arrayWithObjects:@"Implement exponential back-off in your retry mechanism. (e.g. if you waited one second before the first retry,Implement exponential back-off in your retry mechanism. (e.g. if you waited one second before the first retry,Implement exponential back-off in your retry mechanism. (e.g. if you waited one second before the first retry,Implement exponential back-off in your retry mechanism. (e.g. if you waited one second before the first retry,finally",nil];


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

    UITableViewCell *cell =
    [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
     cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];

if(indexPath.section == 1)
    {

        formulaText = [[UITextView alloc]init];
        formulaText.font = [UIFont fontWithName:@"Bitter-Regular" size:15.0f];
        formulaText.translatesAutoresizingMaskIntoConstraints = NO;
        formulaText.backgroundColor = [UIColor lightGrayColor];
        formulaText.scrollEnabled = NO;
        [cell.contentView addSubview:formulaText];

        NSDictionary * views = NSDictionaryOfVariableBindings(formulaText);

        NSArray * formulaH = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[formulaText]-10-|"
                                                                        options:0
                                                                        metrics:nil
                                                                          views:views];

        NSArray * formulaV = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-5-[formulaText]-5-|"
                                                                     options:0
                                                                     metrics:nil
                                                                       views:views];

        [cell.contentView addConstraints:formulaH];
        [cell.contentView addConstraints:formulaV];

        formulaText.text = [formulaArray objectAtIndex:0];

        #define kMaxHeight 100.f
        formulaText.contentSize = [formulaText.text sizeWithFont:[UIFont fontWithName:@"Bitter-Regular" size:15.0]
                                                   constrainedToSize:CGSizeMake(100, kMaxHeight)
                                                       lineBreakMode:NSLineBreakByWordWrapping];

    }

    if (indexPath.section == 2)
    {
        aboutText = [[UITextView alloc]init];
        aboutText.font = [UIFont fontWithName:@"Bitter-Regular" size:15.0f];
        aboutText.translatesAutoresizingMaskIntoConstraints = NO;
        aboutText.backgroundColor = [UIColor darkGrayColor];
        aboutText.scrollEnabled = NO;
        [cell.contentView addSubview:aboutText];

        NSDictionary * views = NSDictionaryOfVariableBindings(aboutText);

        NSArray * formulaH = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[aboutText]-10-|"
                                                                     options:0
                                                                     metrics:nil
                                                                       views:views];

        NSArray * formulaV = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-5-[aboutText]-5-|"
                                                                     options:0
                                                                     metrics:nil
                                                                       views:views];

        [cell.contentView addConstraints:formulaH];
        [cell.contentView addConstraints:formulaV];

        aboutText.text = [aboutArray objectAtIndex:0];
        #define kMaxHeight 100.f
        aboutText.contentSize = [aboutText.text sizeWithFont:[UIFont fontWithName:@"Bitter-Regular" size:15.0]
                                               constrainedToSize:CGSizeMake(100, kMaxHeight)
                                               lineBreakMode:NSLineBreakByWordWrapping];
    }

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //return 200;

    if([indexPath section] == 0)
    {
        return  200;
    }

    else if([indexPath section] == 1)
    {
        NSString *cellText = [formulaArray objectAtIndex:0];
        UIFont *cellFont = [self fontForCell];
        CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
        CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:NSLineBreakByWordWrapping];
        return labelSize.height + 20;
    }

    else
    {
        NSString *cellText = [aboutArray objectAtIndex:0];
        UIFont *cellFont = [self fontForCell];
        CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
        CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:NSLineBreakByWordWrapping];
        return labelSize.height + 20;
    }
}

但是这里的公式数组所有 text 数据没有显示请帮帮我

【问题讨论】:

    标签: ios objective-c


    【解决方案1】:

    试试这个。

      - (CGSize )getLabelSizeForString:(NSString *)message      withLabel:(UILabel *)label{
         CGSize size = [message sizeWithFont:label.font constrainedToSize:CGSizeMake(label.bounds.size.width, 99999) lineBreakMode:NSLineBreakByWordWrapping];
          return size;
       }
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CustomCell"];
    NSString *message = [[self.dataArray objectAtIndex:indexPath.row]valueForKey:Message];
    CGSize size = [[GlobalConstants sharedInstance] getLabelSizeForString:message withLabel:cell.aboutLabel];
    
        return size.height;
       }
    

    或者你可以引用链接 http://www.fantageek.com/1468/ios-dynamic-table-view-cells-with-varying-row-height-and-autolayout/

    【讨论】:

    • 使用此代码,我们无法根据 textlength 动态增加 tableview 单元格和 textview 的高度
    • 我在我的代码中按照上面链接中给出的所有步骤执行此操作。
    【解决方案2】:

    我觉得这个功能

    CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:NSLineBreakByWordWrapping];
    

    在您的身高 ForRowAtIndexPath 中已弃用。

    尝试使用 NSFontAttribute 代替它。在您的情况下,它看起来像这样:

    CGRect textRect = [cellText boundingRectWithSize:constraintSize
                                     options:NSStringDrawingUsesLineFragmentOrigin
                                  attributes:@{NSFontAttributeName:cellFont}
                                     context:nil];
    CGSize labelSize = textRect.size;
    

    如果您想动态增加它,您需要刷新已编辑的特定单元格。在你的

    - (void)textViewDidChange:(UITextView *)textView
    

    您需要刷新您的单元格。

      [tableView reloadRowsAtIndexPaths:@[yourIndex] withRowAnimation:UITableViewRowAnimationNone];
    

    【讨论】:

    • 我想使用这段代码根据文本长度动态增加表格视图单元格和文本视图高度
    • 好的,抱歉,我不明白您当前的问题。如果您想动态增加它,您需要刷新已编辑的特定单元格。 [tableView reloadRowsAtIndexPaths:@[yourIndex] withRowAnimation:UITableViewRowAnimationNone];
    【解决方案3】:
        - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        return formulaArray.count;
    }
    
        - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
        MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellIdentifier"];
    
    
        cell.myTextView.text     = formulaArray[indexPath.row];
    
        [cell.contentView layoutIfNeeded];
        return cell;
    }
    
        - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
        return UITableViewAutomaticDimension;
    }
    
        - (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
        return UITableViewAutomaticDimension;
    }
    

    您可以通过情节提要轻松实现这些。

    1. 在单元格中拖动 textview 并仅添加顶部、底部、前导和尾随约束。

    2. 取消选中为 textview 启用滚动。

    3. 然后实现这些方法

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-12
      • 1970-01-01
      • 1970-01-01
      • 2017-10-28
      • 1970-01-01
      • 2016-04-17
      相关资源
      最近更新 更多