Senario A: If I set the label content in cellForRowAtIndexPath
, the cell correctly get resized.
Senario A:如果我在cellForRowAtIndexPath中设置标签内容,则正确调整单元格的大小。
Senario B: If I change the text content in custom action in cell, the cell sized does not get changed.(I do call setNeedsLayout + layoutIfNeeded)
Senario B:如果我在单元格中更改自定义操作中的文本内容,则单元格大小不会更改。(我确实调用setNeedsLayout + layoutIfNeeded)
How to fix this?
如何解决这个问题?
EDIT:
1) I have set, myTableView.estimatedRowHeight = 71.0
myTableView.rowHeight = UITableViewAutomaticDimension
1)我已设置,myTableView.estimatedRowHeight = 71.0 myTableView.rowHeight = UITableViewAutomaticDimension
2) I have correctly added auto layout constraints.
2)我已正确添加自动布局约束。
8 个解决方案
#1
4
After you change the text of the cell, just reload that particular cell or simply call mainTableView.reloadData()
.
更改单元格的文本后,只需重新加载该特定单元格或只需调用mainTableView.reloadData()。
To reload that cell-
要重新加载该单元格 -
//indexPath is indexPath of cell you just changed label of
mainTableView.reloadRows(at: indexPath, with: .automatic)
#2
4
I was running into this issue, and my problem was that I was constraining the content to self
(the UITableViewCell
) and not to self.contentView
(the contentView
OF the cell). Hope this helps someone else who has built their cells all in code!
我遇到了这个问题,我的问题是我将内容限制为自我(UITableViewCell)而不是self.contentView(单元格的contentView)。希望这可以帮助其他人在代码中构建他们的单元格!
#3
1
First of all, I don't specifically know what was your action on UITableViewCell
. So, I assume I do that in UITableViewCell
selection.
首先,我不知道你对UITableViewCell的行动是什么。所以,我假设我在UITableViewCell选择中这样做。
The below answer only work on iOS 9 and above
以下答案仅适用于iOS 9及更高版本
But, for some reason, it failed to do it in iOS 8 until it scroll. So, I will update the answer for iOS 8.
但是,出于某种原因,它在iOS 8中无法在滚动之前完成。所以,我将更新iOS 8的答案。
I have seen you have used UITableView
's estimatedRowHeight
and rowHeight
at your project. So,
我已经看到你在项目中使用了UITableView的estimatedRowHeight和rowHeight。所以,
Please check the following
请检查以下内容
- Make sure
UITableView
'sestimatedRowHeight
androwHeight
include insideviewDidLoad()
- Make sure your
UILabel
lines set to 0
确保UITableView的estimatedRowHeight和rowHeight包含在viewDidLoad()中
确保您的UILabel线设置为0
- Make sure there is no constraints about height for your UILabel and let the constraints be like that :
确保您的UILabel没有关于高度的限制,并让约束如下:
If there are other component also included, make sure only bottom and top space constraints included or top space to container margin and bottom space to container margin.
如果还包括其他组件,请确保仅包括底部和顶部空间限制或顶部空间到容器边距和底部空间到容器边距。
- Every time that you want to update the cell, you have to reload tableView no matter what your current situation will be.
每次要更新单元格时,无论当前情况如何,都必须重新加载tableView。
So, don't say anything yet before you try this sample project, @Rikh
answer still work. May be you are going in wrong direction. Here's the solution. Please do as I said steps by steps and let me know if that didn't work out. You might need to share your sample project which is causing.
所以,在你尝试这个示例项目之前还没有说什么,@ Rikh的答案仍然有用。可能是你走错了方向。这是解决方案。请按照我说的步骤进行操作,如果不能解决,请告诉我。您可能需要共享正在导致的示例项目。
Sample Demo - DynamicCellDemo
示例演示 - DynamicCellDemo
UPDATE for iOS 8 : update the following code for iOS 8 users
适用于iOS 8的更新:为iOS 8用户更新以下代码
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
if #available(iOS 9, *) {
// do nothing
} else {
tblDynamic.reloadData()
}
}
#5
0
what you can do is set the AutoLayout constraints for the label in present in the cell from all the sides that is from Top, Bottom, Leading and Trailing. Then add the following UITableViewDelegate method to your class.
您可以做的是为来自顶部,底部,前导和尾随的所有边的单元格中的标签设置AutoLayout约束。然后将以下UITableViewDelegate方法添加到您的类中。
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return 500
}
This will do the job. As now content in table view cell automatically adjusts the height of the cell.
这将完成这项工作。现在,表视图单元格中的内容会自动调整单元格的高度。
#6
0
Try this if it works:
如果有效,请尝试此操作:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
#7
0
In my case, in the same cell I had an imageView in the top left corner with a "center vertically in container" constraint, and a "top space container" constraint.
在我的例子中,在同一个单元格中,我在左上角有一个带有“中心垂直容器”约束的imageView,以及一个“顶部空间容器”约束。
Obviously to satisfy this two constraint the cell must have an height equal to:
显然,要满足这两个约束,单元格的高度必须等于:
(height of the imageView / 2) + (length of the top space container constraint).
(imageView / 2的高度)+(顶部空间容器约束的长度)。
This height is not enough to fit the label text, so my label had only 1 line visible.
此高度不足以适合标签文本,因此我的标签只有1行可见。
After I have deleted the imageView top constraint all went to the right place, in my case i wanted the image to be centered, if the image had to stay in the top left corner I had to take off the "center vertically in container" constraint.
我删除了imageView顶部约束后,所有都去了正确的地方,在我的情况下,我希望图像居中,如果图像必须留在左上角,我必须取下“容器中的垂直中心”约束。
I hope this can help someone.
我希望这可以帮助别人。
#8
-2
Add the following in your viewDidLoad()
在viewDidLoad()中添加以下内容
-(void)viewDidLoad{
[super viewDidLoad];
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 140
}
#1
4
After you change the text of the cell, just reload that particular cell or simply call mainTableView.reloadData()
.
更改单元格的文本后,只需重新加载该特定单元格或只需调用mainTableView.reloadData()。
To reload that cell-
要重新加载该单元格 -
//indexPath is indexPath of cell you just changed label of
mainTableView.reloadRows(at: indexPath, with: .automatic)
#2
4
I was running into this issue, and my problem was that I was constraining the content to self
(the UITableViewCell
) and not to self.contentView
(the contentView
OF the cell). Hope this helps someone else who has built their cells all in code!
我遇到了这个问题,我的问题是我将内容限制为自我(UITableViewCell)而不是self.contentView(单元格的contentView)。希望这可以帮助其他人在代码中构建他们的单元格!
#3
1
First of all, I don't specifically know what was your action on UITableViewCell
. So, I assume I do that in UITableViewCell
selection.
首先,我不知道你对UITableViewCell的行动是什么。所以,我假设我在UITableViewCell选择中这样做。
The below answer only work on iOS 9 and above
以下答案仅适用于iOS 9及更高版本
But, for some reason, it failed to do it in iOS 8 until it scroll. So, I will update the answer for iOS 8.
但是,出于某种原因,它在iOS 8中无法在滚动之前完成。所以,我将更新iOS 8的答案。
I have seen you have used UITableView
's estimatedRowHeight
and rowHeight
at your project. So,
我已经看到你在项目中使用了UITableView的estimatedRowHeight和rowHeight。所以,
Please check the following
请检查以下内容
- Make sure
UITableView
'sestimatedRowHeight
androwHeight
include insideviewDidLoad()
- Make sure your
UILabel
lines set to 0
确保UITableView的estimatedRowHeight和rowHeight包含在viewDidLoad()中
确保您的UILabel线设置为0
- Make sure there is no constraints about height for your UILabel and let the constraints be like that :
确保您的UILabel没有关于高度的限制,并让约束如下:
If there are other component also included, make sure only bottom and top space constraints included or top space to container margin and bottom space to container margin.
如果还包括其他组件,请确保仅包括底部和顶部空间限制或顶部空间到容器边距和底部空间到容器边距。
- Every time that you want to update the cell, you have to reload tableView no matter what your current situation will be.
每次要更新单元格时,无论当前情况如何,都必须重新加载tableView。
So, don't say anything yet before you try this sample project, @Rikh
answer still work. May be you are going in wrong direction. Here's the solution. Please do as I said steps by steps and let me know if that didn't work out. You might need to share your sample project which is causing.
所以,在你尝试这个示例项目之前还没有说什么,@ Rikh的答案仍然有用。可能是你走错了方向。这是解决方案。请按照我说的步骤进行操作,如果不能解决,请告诉我。您可能需要共享正在导致的示例项目。
Sample Demo - DynamicCellDemo
示例演示 - DynamicCellDemo
UPDATE for iOS 8 : update the following code for iOS 8 users
适用于iOS 8的更新:为iOS 8用户更新以下代码
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
if #available(iOS 9, *) {
// do nothing
} else {
tblDynamic.reloadData()
}
}
#4
#5
0
what you can do is set the AutoLayout constraints for the label in present in the cell from all the sides that is from Top, Bottom, Leading and Trailing. Then add the following UITableViewDelegate method to your class.
您可以做的是为来自顶部,底部,前导和尾随的所有边的单元格中的标签设置AutoLayout约束。然后将以下UITableViewDelegate方法添加到您的类中。
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return 500
}
This will do the job. As now content in table view cell automatically adjusts the height of the cell.
这将完成这项工作。现在,表视图单元格中的内容会自动调整单元格的高度。
#6
0
Try this if it works:
如果有效,请尝试此操作:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
#7
0
In my case, in the same cell I had an imageView in the top left corner with a "center vertically in container" constraint, and a "top space container" constraint.
在我的例子中,在同一个单元格中,我在左上角有一个带有“中心垂直容器”约束的imageView,以及一个“顶部空间容器”约束。
Obviously to satisfy this two constraint the cell must have an height equal to:
显然,要满足这两个约束,单元格的高度必须等于:
(height of the imageView / 2) + (length of the top space container constraint).
(imageView / 2的高度)+(顶部空间容器约束的长度)。
This height is not enough to fit the label text, so my label had only 1 line visible.
此高度不足以适合标签文本,因此我的标签只有1行可见。
After I have deleted the imageView top constraint all went to the right place, in my case i wanted the image to be centered, if the image had to stay in the top left corner I had to take off the "center vertically in container" constraint.
我删除了imageView顶部约束后,所有都去了正确的地方,在我的情况下,我希望图像居中,如果图像必须留在左上角,我必须取下“容器中的垂直中心”约束。
I hope this can help someone.
我希望这可以帮助别人。
#8
-2
Add the following in your viewDidLoad()
在viewDidLoad()中添加以下内容
-(void)viewDidLoad{
[super viewDidLoad];
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 140
}