Storyboard_ High-contrast custom high cell, requires several additional steps (IOS8 is supported at the beginning)
// 告诉tableView所有cell的真实高度是自动计算(根据设置的约束来计算)self.tableView.rowHeight = UITableViewAutomaticDimension;// 告诉tableView所有cell的估算高度self.tableView.estimatedRowHeight = 44;
- To change the cell height, simply change the value of the constraint.
If you want to support iOS8 before
- If there is a wrap label inside the cell, you need to set the Preferredmaxlayoutwidth property
- (void)awakeFromNib{ // 手动设置文字的最大宽度(目的是:让label知道自己文字的最大宽度,进而能够计算出自己的frame) self.text_label.preferredMaxLayoutWidth = [UIScreen mainScreen].bounds.size.width - 20;}
- Set the cell estimate height for TableView
// 告诉tableView所有cell的估算高度(设置了估算高度,就可以减少tableView:heightForRowAtIndexPath:方法的调用次数)self.tableView.estimatedRowHeight = 200;
- Calculating the cell height in the proxy method
Xmgstatuscell *cell;-(CGFloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (Nsindexpath *) indexpath{Create a cell (the role of the cell: Calculate the cell's height based on the child controls that are laid out by the model data)if (!cell) {cell = [TableView dequeuereusablecellwithidentifier:id];}Set model data cell. Status =Self. Statuses[indexpath. row];return cell.height;} -(cgfloat) height{//Force layout of all child controls inside the cell ( Label based on the number of words to calculate their true size) [self layoutifneeded]; //calculate cell height if (self.status.picture) {return CGRectGetMaxY ( Span class= "Hljs-keyword" >self.pictureimageview.frame) + 10;} else {return cgrectgetmaxy (self .text_label.frame) + 10;}}
Ios-ui Control UITableView (iii)-custom cell with unequal height