ios - 如何在ios中根据textlength增加UItableView Cell和UITextView高度
<p><p>嗨,我是 Ios 的初学者,在我的项目中,我在 UItableview 单元格上添加了 UItextView,并根据 textlength 自动增加 UItextview 和 UItableview 单元格的高度,但根据我的代码,所有 textdata 都没有显示在 textview 上,有些数据丢失了请帮帮我某一个</p>
<h2>这是我的代码:</h2>
<pre><code> formulaArray = ;
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"cellIdentifier";
UITableViewCell *cell =
;
cell = [initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
if(indexPath.section == 1)
{
formulaText = [init];
formulaText.font = ;
formulaText.translatesAutoresizingMaskIntoConstraints = NO;
formulaText.backgroundColor = ;
formulaText.scrollEnabled = NO;
;
NSDictionary * views = NSDictionaryOfVariableBindings(formulaText);
NSArray * formulaH = -10-|"
options:0
metrics:nil
views:views];
NSArray * formulaV = -5-|"
options:0
metrics:nil
views:views];
;
;
formulaText.text = ;
#define kMaxHeight 100.f
formulaText.contentSize =
constrainedToSize:CGSizeMake(100, kMaxHeight)
lineBreakMode:NSLineBreakByWordWrapping];
}
if (indexPath.section == 2)
{
aboutText = [init];
aboutText.font = ;
aboutText.translatesAutoresizingMaskIntoConstraints = NO;
aboutText.backgroundColor = ;
aboutText.scrollEnabled = NO;
;
NSDictionary * views = NSDictionaryOfVariableBindings(aboutText);
NSArray * formulaH = -10-|"
options:0
metrics:nil
views:views];
NSArray * formulaV = -5-|"
options:0
metrics:nil
views:views];
;
;
aboutText.text = ;
#define kMaxHeight 100.f
aboutText.contentSize =
constrainedToSize:CGSizeMake(100, kMaxHeight)
lineBreakMode:NSLineBreakByWordWrapping];
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//return 200;
if( == 0)
{
return200;
}
else if( == 1)
{
NSString *cellText = ;
UIFont *cellFont = ;
CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
CGSize labelSize = ;
return labelSize.height + 20;
}
else
{
NSString *cellText = ;
UIFont *cellFont = ;
CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
CGSize labelSize = ;
return labelSize.height + 20;
}
}
</code></pre>
<p>但这里的公式数组所有文本<img src="/image/x31Kr.png" alt="enter image description here"/>数据不显示请帮帮我</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>试试这个。 </p>
<pre><code>- (CGSize )getLabelSizeForString:(NSString *)message withLabel:(UILabel *)label{
CGSize size = ;
return size;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
CustomCell *cell = ;
NSString *message = [valueForKey:Message];
CGSize size = [ getLabelSizeForString:message withLabel:cell.aboutLabel];
return size.height;
}
</code></pre>
<p>或者你可以引用链接
<a href="http://www.fantageek.com/1468/ios-dynamic-table-view-cells-with-varying-row-height-and-autolayout/" rel="noreferrer noopener nofollow">http://www.fantageek.com/1468/ios-dynamic-table-view-cells-with-varying-row-height-and-autolayout/</a> </p></p>
<p style="font-size: 20px;">关于ios - 如何在ios中根据textlength增加UItableView Cell和UITextView高度,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/31538566/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/31538566/
</a>
</p>
页:
[1]