Ios-ui Control UITableView (iii)-custom cell with unequal height

Source: Internet
Author: User

Storyboard_ High-contrast custom high cell, requires several additional steps (IOS8 is supported at the beginning)
    • To add a spacing constraint between a child control and a Contentview

    • Set the true implementation height of the Tableviewcell and estimate the row height

// 告诉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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.