我们如何在 uitableview 单元格中设置标签约束?

how we set label Constraints in uitableview cell?

我想在表格视图单元格中设置标签约束。 当我在设备上设置标签约束和 运行 时,自动布局无法在 iPhone 5,6 和 iPhone 7 plus 等不同设备上工作。

Here is the screenshot

如何设置自动布局约束以使布局适用于所有设备?

谢谢!

您必须为单元格提供估计的行高或将其设置为自动。

示例:-

tableView.estimatedRowHeight = 120.0
tableView.rowHeight = UITableViewAutomaticDimension

或者像这样:-

func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat 
{
 return 240
}

您需要设置以下参数

tableView.rowHeight = UITableViewAutomaticDimension;
tableView.estimatedRowHeight = 140.0; //some values according to the your requirement 

试试这个 -

将标签拖入单元格。 假设您希望标签的每一侧都有 10 个像素的边距。为所有(顶部、底部、前导、尾随)设置一个常量 10,然后单击添加 4 个约束。如下图

现在,如果您只有标签宽度方面的问题,请执行以下操作 -

viewWillAppear方法

tableView.rowHeight = UITableViewAutomaticDimension;
tableView.estimatedRowHeight = 50.0;

然后使用以下方法 -

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath {

     return  UITableViewAutomaticDimension;
}

你的问题现在应该已经解决了。