After 1.ios8, use storyboard or xib to customize the unequal cell:
A few additional steps are required to compare a custom high-level cell (IOS8 is supported at the beginning)
- Add a spacing constraint between a child control and a Contentview (cell's Contentview) ( requires code control constraints )
- Set the true implementation height of the Tableviewcell and estimate the row height
// The following two lines of code are Apple became self0sizing technology, but only in iOS8 and its subsequent application // tell TableView that the true height of all cells is calculated automatically ( calculated based on the constraints set )self.tableView.rowHeight = uitableviewautomaticdimension; // tell TableView The estimated height of all cells ;
Before 2.ios8, use storyboard or xib to customize the unequal cell:
- If there is a wrap label inside the cell, you need to set the Preferredmaxlayoutwidth property
- The label knows its maximum width because it is displayed, and it calculates the maximum width preferredmaxlayoutwidth according to its own constraints or width constraints . It then calculates the actual height of the label based on the maximum width and content. But the premise of all this is that when the label is to be displayed , if the label is not displayed, preferredmaxlayoutwidthis not computed, that is, preferredmaxlayoutwidth = 0. You need to manually set the preferredmaxlayoutwidth
- awakefromnib in preferredmaxlayoutwidth to assign the value.
-(void) awakefromnib{ // manually set the maximum width of the text (for the purpose of letting the label know the maximum width of its own text, and thus to be able to calculate its own frame) }
- Set the cell estimate height for TableView
-(void) viewdidload { [super Viewdidload]; // tell TableView The estimated height of all cells (the estimated height is set, you can reduce the Tableview:heightforrowatindexpath: number of calls to the method) $ ;}
Estimating the effect of heights: Because TableView inherits from Uiscrollview, TableView needs to calculate contentsize based on the cell's height. If you do not set an estimate height, the program starts by calling multiple Heightforrowatindexpath: Method (the number equals the count of the data model). However, if the estimated height is set, the program calculates the contentsize based on the given estimated height (calculated by estimating the height * Count of the data model). The Heightforrowatindexpath is called only when the cell is displayed: The method calculates the true height of the cell, so that the Heightforrowatindexpath: the number of calls to the method is greatly reduced, and the performance is greatly improved.
- The cell provides a property of height that returns the height of the cell
- (cgfloat) height{ // Force layout All child controls inside the cell (label calculates its true size based on the number of words) [self layoutifneeded]; //iftenelse;}}
- Calculating the cell height in the proxy method
Xmgstatuscell *cell;-(CGFloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (Nsindexpath *) indexpath{//creates a cell that calculates the height of a cell based on all the child controls laid out by the model data, but because the cell is not displayed, you need to manually set the maximum width of the label in the cell .//The cell acts only to calculate and return the cell's height, not to return the cell, so the cell is soy sauce, and the cell is not displayed on the TableView. Since it will not be recycled to the TableView cache pool (because only cells that have been shown to TableView and have left the screen will be recycled to the cache pool) if(!cell) {Cell=[TableView Dequeuereusablecellwithidentifier:id]; } //set up model dataCell.status =Self.statuses[indexpath.row]; returncell.height;}
Custom unequal height Cell-storyboard or xib custom unequal cell