So basically, I want to embed a UITableView
inside UITableViewCell
.
I used UIStackView
to use its AutoLayout
power!
Anyway, each row of "Inner UITableView" will consist of a fixed sized UIImageView
and a UILabel
of lines = 0
(i.e. not fixed)
UIStackView
(horizontal) -> UIImageView
+ UILabel
Requirements:
- The size of each row/cell for InnerTableView should be dynamic due to random text being provided from the server. Thus making the whole Inner
UITableView
's height toautomaticDimension
- The text for the
UILabel
above InnerUITableView
is also dynamic. (thus makes sense to useUIStackView
in the first place)
Note: The Main UITableView
consist of multiple cells of different types (this is basically a BotChatBot)
Also: estimatedHeight
can be around 40 points.
Problem:
The InnerTableView being dynamically sized leads to its cellForRowAt
method not being called for once, thus not allowing UIStackView
to increase its length due to no change in contentSize
.
In order to fix the above problem, I tried:
- Setting up initial height constraint for UITableVIew to 10(some random number),
estimatedRowHight
for InnerTableView being 50, thus setting up height of UITableView astableViewInsideCellHeightConstraint?.constant = tableViewInsideCell.contentSize.height
which somehow gives me some room but it's still not reliable when height for a row exceeds estimated height of 40
P.S layoutIfNeeded()
were triggered for TableView
, StackView
and ParentCell
I also have another way of solving the above problem:
- Using estimated height, multiply the constant(estimatedHeight) by number of rows to be displayed i.e. list size
- Change
heightConstraint
forInner UITableView
with the calculated one, - With each iteration, calculate the total "REQUIRED" height (using
+=visibleCells[i].frame.height
) of UITableView (as each iteration will provide the size of the cell(content size) - Update the height constraint of
Inner UITableView
if heightForTableView == nil {
if indexPath.row == listDict!.count - 1 {
var heightOfTableView: CGFloat = 0.0
let cells = tableView.visibleCells
for visibleCells in cells {
heightOfTableView += visibleCells.frame.height
}
let newHeight = heightOfTableView + cell.frame.height
reloadMe(newHeight)
}
}
The above fix leads to visible jerk in the UI :[
Question:
Is there any concrete solution to the above problem? I tried searching it over and over again.. can't really find any strong fix!