• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ios - 如何在ios中根据textlength增加UItableView Cell和UITextView高度

[复制链接]
菜鸟教程小白 发表于 2022-12-13 09:07:14 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

嗨,我是 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 *)tableViewUITableView *)tableView
         cellForRowAtIndexPathNSIndexPath *)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)tableViewUITableView *)tableView heightForRowAtIndexPathNSIndexPath *)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;
    }
}

但这里的公式数组所有文本enter image description here数据不显示请帮帮我



Best Answer-推荐答案


试试这个。

  - (CGSize )getLabelSizeForStringNSString *)message      withLabelUILabel *)label{
     CGSize size = [message sizeWithFont:label.font constrainedToSize:CGSizeMake(label.bounds.size.width, 99999) lineBreakMode:NSLineBreakByWordWrapping];
      return size;
   }
- (CGFloat)tableViewUITableView *)tableView heightForRowAtIndexPathNSIndexPath *)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/

关于ios - 如何在ios中根据textlength增加UItableView Cell和UITextView高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31538566/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap