简体   繁体   English

uitableviewcell中的自动布局问题

[英]Problems with autolayout in uitableviewcell

I am having some hard time as usual getting the constraints correct. 与往常一样,我遇到了一些困难,无法正确解决约束条件。 This is how my tableview works: 这是我的表视图的工作方式:

This is the default display. 这是默认显示。 The height of the cell is 57. 单元格的高度是57。 在此处输入图片说明 When you click on a cell, i change the selected cells height to 90 so it displays this information: 当您单击一个单元格时,我将所选单元格的高度更改为90,因此它将显示以下信息: 在此处输入图片说明

My problem is to set the constraints so it looks exactly as it does in both images. 我的问题是设置约束,使其看起来与在两个图像中都一样。 What is the best solution to this? 最好的解决方案是什么?

Would it be easier if i made two views and placed them inside of my uitableview so the constraints will be set for the specific view instead of the cell's frame which is being changed on user click etc. I uploaded my project if anybody wants a better overview of what is going on: 如果我创建了两个视图并将它们放置在uitableview内会更容易,那么将为特定视图设置约束,而不是在用户单击等时更改单元格的框架。我上传了我的项目,如果有人想要更好的概述发生了什么:

https://ufile.io/u2y5p https://ufile.io/u2y5p

您还可以将信息放入UIStackView ,直到单击该单元格为止,它们才不可见,使用UITableViewAutomaticDimension可以使单元格的高度动态化,因此,当您使这些信息可见时,重新加载UITableView可以使单元格的高度更高

Suppose you are displaying the data on TableView from an array of models. 假设您要在模型模型的TableView上显示数据。 Keep a property in your model class named as "isExpanded" and set its value by default false. 在模型类中保留一个名为“ isExpanded”的属性,并默认将其值设置为false。

Now, use below code: 现在,使用以下代码:

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    let dataModel = self.arrayOfModels[indexPath.row]
    return dataModel.isExpanded ? 100 : 50
}

On select a row: 在选择一行:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let dataModel = self.arrayOfModels[indexPath.row]
    dataModel.isExpanded = true
    self.tableView.reloadRows(at: [indexPath], with: .automatic)
}

Let me know if you have any doubt, or need more clarification. 如果您有任何疑问或需要进一步澄清,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM