Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

uitablviewcell dynamic height is not correct from systemLayoutSizeFittingSize

Tags:

I am using xib & auto layout to construct my custom cell,in my cell ,there is a multi line label. an in the tableview's heightForRowAtIndexPath, i will use enter image description here

1 dequeueReusableCellWithIdentifier to get a cell

2 then call [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize] to get the correct height .

enter image description here But in debug , I found that when heightForRowAtIndexPath is called ,my cell's width is not the tableview's width, the cell's width is the same as the width of xib file (because in xcode6 ,you can set the xib size to any). so the height got from systemLayoutSizeFittingSize is not correct . how can I fix this problem to get the correct cell height

the size got from the systemLayoutSizeFittingSize: is not correct ,this size may have the width 340+ (while the device is iPhone5 whose width is 320),and the 340+ is accually the cell's xib file 's width

Finally, I got the solution ,thanks Ashish Gabani for help ,in fact ,the solution include 2 step: 1 reset the cell frame to your tableView size 2 call cell's layoutSubViews before you calling systemLayoutSizeFittingSize, and In your custom cell ,you need to rewrite the layoutSubViews method. Here is the code for fix this problem , I use the red square to mark them ,enjoy it

enter image description hereenter image description here