Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/103.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用自动布局的UItableviewcell中的多行UIlabel_Ios_Iphone_Uitableview_Autolayout_Uilabel - Fatal编程技术网

Ios 使用自动布局的UItableviewcell中的多行UIlabel

Ios 使用自动布局的UItableviewcell中的多行UIlabel,ios,iphone,uitableview,autolayout,uilabel,Ios,Iphone,Uitableview,Autolayout,Uilabel,我已经将setMaXPreferredLayoutWidth设置为UILabel,并向UILabel添加了所有必需的约束,现在我可以在我的UItableviewcell中看到多行UILabel,但我的问题是,基于MaxpreferredLayoutWidth值 当我将MaxPreferreLayoutWidth设置为200时,我得到了这个值 当我将MaxPreferreLayoutWidth设置为100时,我得到了这个值 通常,ui标签高度取决于首选MaxLayoutWidth。 有谁能告诉

我已经将
setMaXPreferredLayoutWidth
设置为
UILabel
,并向
UILabel
添加了所有必需的约束,现在我可以在我的
UItableviewcell
中看到多行
UILabel
,但我的问题是,基于
MaxpreferredLayoutWidth

当我将
MaxPreferreLayoutWidth
设置为200时,我得到了这个值

当我将
MaxPreferreLayoutWidth
设置为100时,我得到了这个值

通常,
ui标签
高度取决于
首选MaxLayoutWidth
。 有谁能告诉我如何删除这个额外的填充并渲染
UIlabel
的确切高度吗?

试试这个代码

CGSize suggestedSize = [textWithMultipleLines sizeWithFont:myLabel.font constrainedToSize:CGSizeMake(width, FLT_MAX) lineBreakMode:NSLineBreakByWordWrapping];//here "width" is the maximum acceptable width of myLabel
CGRect labelFrame = myLabel.frame;
labelFrame.size = suggestedSize;
[myLabel setFrame:labelFrame];
而且

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
{
     //get the string assigned to label
     CGSize suggestedSize = [stringAssignedToLabel sizeWithFont:myLabel.font constrainedToSize:CGSizeMake(width, FLT_MAX) lineBreakMode:NSLineBreakByWordWrapping];
     return suggestedSize.height+5;
}

最简单的方法是扩展您自己的UITableViewCell类,并在其中创建UILabel,同时创建公共getMethod:

接口:

实施:

TableView文件:

首先设置标签的文本,然后设置//行数和大小调整//

您需要在适当的时间重新加载tableView数据


我没有时间测试它,但我希望它能工作。

我们必须设置tableviewcell的框架,同时在
heightForRowAtIndexPath中计算单元格的高度。

例:

然后在
UItableviewcell
layoutSubviews
方法中,我们必须设置
preferredMaxLayoutwidth

例:


我正在代码中使用自动布局。。但在你的代码snipper中,你要求我设置框架。有没有办法使用自动布局解决它?我在代码中使用自动布局,由于某些原因,我无法在代码中禁用自动布局。有没有办法在启用自动布局的情况下解决它?只是为了更清楚地说明我在代码中使用自动布局。
- (CGFloat)getLabelHeight;
- (CGFloat)getLabelHeight {
  return label.frame.size.height
}
-(CGFloat)heightForRowAtIndexPath {
    return [cell getLabelHeight]
}

- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath)indexPath {

   your_cell_instance = [[Your_Cell_Class alloc]initWithTableViewCellStyle:UITableViewCellStyleDefault andReuseID:your_reuse_id];

   your_cell_instance.text = @"Your Long Text"
   [your_cell_instance setNumberOfLines:0];
   [your_cell_instance sizeToFit];

   return your_cell_instance;

}
[tableViewCell setFrame:CGRectMake(0, 0, CGRectGetWidth(self.tableView.bounds), CGRectGetHeight(tableViewCell.bounds))];
[self.multilineLabel setPreferredMaxLayoutWidth:CGRectGetWidth(self.bounds)];