嗨,我是 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;
}
}
但这里的公式数组所有文本数据不显示请帮帮我
试试这个。
- (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/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |