gpt4 book ai didi

ios - 如何根据 ios 中的文本长度增加 UItableView Cell 和 UITextView 的高度

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:14:01 25 4
gpt4 key购买 nike

嗨,我是 Ios 的初学者,在我的项目中,我在 UItableview 单元格上添加了 UItextView,并且基于 textlength 自动 UItextview 和 UItableview 单元格高度需要增加,但根据我的代码,所有文本数据都没有显示在 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;
}
}

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

最佳答案

试试这个。

  - (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/

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com