For a normal cell, we can generally give a fixed height based on the type of cell, in which case the layout of our control and the amount of space needed to display it are predictable for us, so it can be implemented directly. , and for some more advanced requirements scenarios (such as the cell display of chat information), we want to show all the information in the cell, we can not return the fixed value in the proxy method of Heightforrowatindexpath.
So, our extra work steps are divided into the following stages:
Step1: Get the appropriate data content
Step2: Depending on the data content, the cell layout
Step3: Calculates the cell's final height according to the cell layout
Step4: Returns the height from the Heightforrowatindexpath.
For a picture, you can reverse the layout height of the image according to the size of the picture, for the text we need the following method (example):
cgsize constraint =cgsizemake(cell_content_width -(Cell_ Content_margin *2), 20000.0f);
cgsize size = [textsizewithfont: [uifontsystemfontofsize :font_size]constrainedtosize: Constraint linebreakmode: Uilinebreakmodewordwrap];
cgfloat height = MAX(size. Height,44.0f);
The height occupied by fixed-width text is calculated by Sizewithfont method.
A simple mathematical calculation of the height occupied by all the elements is then obtained and the cell height is returned from the delegate.
UITableView Adaptive Complex Cell construction ideas (this scheme is compatible with iOS8 below, if only support iOS8, there are more advanced programs, please look forward to the sequel)