ios - Update cell height after it is rendered -


i've set uitableviewcell uitableviewautomaticdimension
tableviewcell has uicollectionview embedded in not scrollable can have variable height based on content of collectionview.

right i've tried render cell , assign height constraint of collectionview variable collectionviewheightconstraint , update height once collectionview rendered in layoutsubviews method

override func layoutsubviews() {     super.layoutsubviews()     self.collectionviewheightconstraint?.constant = self.collectionview!.contentsize.height } 

this collectionview constraints (using cartography) :

    self.contentview.addsubview(self.collectionview)        self.contentview.addsubview(self.holdingview)     constrain(self.holdingview!, self.contentview) {         holderview, container in         holderview.top == container.top         holderview.left == container.left         holderview.right == container.right         holderview.bottom == container.bottom     }     constrain(self.collectionview!, self.holdingview) {         collectionview, containerview in         collectionview.top == containerview.top         collectionview.left == containerview.left         collectionview.right == containerview.right         collectionviewheightconstraint = collectionview.height == collectionviewheight         containerview.bottom == collectionview.bottom     } 

but not seem update cell height.

is there way update same?

edit
not duplicate question suggested people , explanation of why in comments below.

since comment small space, i'll put here:

note: don't have set height constraint in viewdidlayoutsubviews somewhere can sure uicollectionview has been set and layout has been setup on whole screen! example, doing in viewdidappear , calling layoutifneeded() work. moving viewdidappear work if have uicollectionview setup before viewdidappear called i.e know uicollectionview datasource beforehand.

fix 1:

try reloading uitableview after setting height , checking if heightconstant != contentsize. use check if height of uicollectionview updated i.e.:

override func layoutsubviews() {     super.layoutsubviews()      if self.collectionviewheightconstraint?.constant != self.collectionview!.contentsize.height{          self.collectionviewheightconstraint?.constant = self.collectionview!.contentsize.height          //to make sure height recalculated         tableview.reloaddata()         //or reload row depending on use case , if know index of row reload :)     } } 

i agree comment , messy, meant use fix and/or check if problem lies actually!

as why 0, happens because uicollectionview hasn't been set yet (cellforitem hasn't been called yet) contentsize isn't calculated!

fix 2:

once datasource uicollectionview has been set, receive data, calculate height uicollectionview contentsize have manually , set once , reload row. if calculation tedious task, set datasource , call reloaddata on uicollectionview. ensure uicollectionview setup , set constraint of cell contentsize , call reloaddata or reloadrow on uitableview.

you can set heightconstraint anytime after uicollectionview has been setup and view has been laid out. need called tableview.reloaddata() afterwards.


Comments