UITabelView advanced (custom Cell), uitabelviewcell
Customize a Cell
When we want to display complex data, such as a button chat interface or a news list, the system's lines cannot meet our requirements. In this case, we can customize this line, show more complex structure styles.
A custom cell is a subclass of UITabelViewCell. It encapsulates the control creation on the cell in the subclass, simplifies the code in UIViewController, and adds the child view control to the contentView of the cell.
How to display the information in the model
The cell declares a model attribute. viewController obtains the Model object and assigns the Model attribute to the cell.
Cell re-writes the setter method of the Model, and assigns the content in the Model object to each control.
M and V do not communicate directly, and C is responsible for communication between M and V.
Multiple types of CELL Mixing
In development, multiple types of cells are commonly used in a tableView. Normally, each type of cell has different layout styles.
Generally, the tabelView: cellForRowAtIndexPath: method determines the type of cell to be used based on different models.
Each type of cell must define different reuse identifiers. During cell reuse, the reuse identifiers are used from the reuse queue to determine the type of cell.
For example, we define a ComposeCel type identifier and a ReceivedCell type identifier.
Then, different types of tables are retrieved from the reuse queue based on different identifiers.
Cell adaptive height
Previously, the cell we used was always the same height or fixed height for a row. However, in actual development, we often need to change the height of the cell dynamically based on the text length in the Model.
First, obtain the text height.
The calculated text height is the same as the font used for label display.
The label width must be the same as the specified width used for calculation, so that the label height happens to be sufficient when the text is displayed in the label.
TableView: heightForRowAtIndexPath: